【问题标题】:Trying to index a Parent results in a nil value尝试索引 Parent 会导致 nil 值
【发布时间】:2018-01-07 19:51:39
【问题描述】:

错误:

16:16:03.496 - Workspace.Storeroom.Pile of Crates.Wood Crate.Script:17:尝试索引字段“父”(零值)

脚本:

local db = true

local clickdetector = script.Parent:WaitForChild('ClickDetector')

clickdetector.MouseClick:Connect(function(plr)

local randomizer = math.random(1,6)

if randomizer == 1 or 2 or 3 then

script.Parent:Destroy()

local gift = game:GetService("ReplicatedStorage"):WaitForChild("Blue Keycard")

if db == true then

db = false

gift:Clone().Parent = plr.Backpack

wait(1)

db = true           

end

end

if randomizer == 4 or 5 then

script.Parent:Destroy()

local gift = game:GetService("ReplicatedStorage"):WaitForChild("Red Keycard")

if db == true then

db = false

gift:Clone().Parent = plr.Backpack

wait(1)

db = true

end

end

if randomizer == 6 then

script.Parent:Destroy()

local gift = game:GetService("ReplicatedStorage"):WaitForChild("Green Keycard")

if db == true then

db = false

gift:Clone().Parent = plr.Backpack

wait(1)

db = true           

end

end

end)

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    如果随机数 == 1 或 2 或 3 那么
    如果随机数 == 4 或 3 则

    这些行不会像您认为的那样工作。它不检查 randomizer 值是否在某些预定义值集中。表达式randomizer == 1 or 2 or 3 计算(取决于randomizer 的确切值)为true2,在if/then 构造中被解释为true

    如果您真的想根据列表检查值,您应该执行以下操作:

    local set1 = {1=true, 2=true, 3=true}
    local set2 = {4=true, 5=true}
    
    --  somewhere much later
    if set1[randomizer] then .. end
    

    从解决这个问题开始。

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2019-10-10
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多