尝试索引Parent会导致nil值

时间:2018-01-07 08:40:38

标签: lua roblox

错误:

  

16:16:03.496 - Crates.Wood Crate.Script的Workspace.Storeroom.Pile:17:尝试索引字段'Parent'(零值)

脚本:

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)

1 个答案:

答案 0 :(得分:0)

  

如果随机数= = 1或2或3则为   如果randomizer == 4或3则

这些线条不会像您认为的那样有效。它不会检查randomizer值是否在某些预定义值集中。表达式randomizer == 1 or 2 or 3评估(取决于randomizer确切的值)true2true构造中的if/then

如果您确实想要检查列表中的值,您应该执行以下操作:

local set1 = {1=true, 2=true, 3=true}
local set2 = {4=true, 5=true}

--  somewhere much later
if set1[randomizer] then .. end

首先解决此问题。