尝试索引父级结果为 nil 值

错误信息:

16:16:03.496 - Workspace.Storeroom.Pile of Crates.Wood Crate.Script:17: 尝试对字段 'Parent' 进行索引 (该值为 nil)

脚本内容:

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)
点赞
用户5675002
用户5675002

这些代码行无法按您想要的方式工作。它不能检查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-07 11:51:39