Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I made a random crate spawn in houses and got, attempt to call a nil value?

Asked by 3 years ago

local crate = game.Lighting.Crates local med = crate.MedBag:GetChildren() local woodencrate = crate.WoodenCrate:GetChildren() local militarycrate = crate.MilitaryCrate:GetChildren() local m = math.random(1,6)

game.Players.PlayerAdded:connect(function(player) local plr = player

while true do
    if m == 1 then
        med:Clone().Parent = script.Parent --Error is here
        med.CFrame = CFrame.new(-206, 4.2, 225)

    end
    if m == 2 then
        med:Clone().Parent = script.Parent
        med.CFrame = CFrame.new(-206, 4.2, 225)

    end
    if m == 3 then
        med:Clone().Parent = script.Parent
        med.CFrame = CFrame.new(-206, 4.2, 225)

    end
    if m == 4 then
        med:Clone().Parent = script.Parent
        med.CFrame = CFrame.new(-206, 4.2, 225)

    end
    if m == 5 then
        woodencrate:Clone().Parent = script.Parent
        woodencrate.CFrame = CFrame.new(-200, 4.2, 240)

    end
    if m == 6 then
        woodencrate:Clone().Parent = script.Parent
        woodencrate.CFrame = CFrame.new(-200, 4.2, 240)

    end
    wait (500) --Restart
    script.Parent:FindFirstChild("MedBag"):Destroy()
    script.Parent:FindFirstChild("MilitaryCrate"):Destroy() 

end

end)

0
means the med variable is nil and fyi, :connect() is deprecated (use :Connect()) 0hsa 193 — 3y
0
so will using :Connected()) fix the issue? ShadowDinoYT 11 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

If you look at what you are defining "med" - local med = crate.MedBag:GetChildren() Why do you need to get the children? If you want to clone the medbag, just state "crate.MedBag". so the correct thing to do:


local med = crate.MedBag while true do if m == 1 then med:Clone().Parent = script.Parent --the only reason the error is here is because it is the first statement that uses it, if you got rid of this statement, the next statement would put out an error med.CFrame = CFrame.new(-206, 4.2, 225) end if m == 2 then med:Clone().Parent = script.Parent med.CFrame = CFrame.new(-206, 4.2, 225) end if m == 3 then med:Clone().Parent = script.Parent med.CFrame = CFrame.new(-206, 4.2, 225) end if m == 4 then med:Clone().Parent = script.Parent med.CFrame = CFrame.new(-206, 4.2, 225) end if m == 5 then woodencrate:Clone().Parent = script.Parent woodencrate.CFrame = CFrame.new(-200, 4.2, 240) end if m == 6 then woodencrate:Clone().Parent = script.Parent woodencrate.CFrame = CFrame.new(-200, 4.2, 240) end wait (500) --Restart script.Parent:FindFirstChild("MedBag"):Destroy() script.Parent:FindFirstChild("MilitaryCrate"):Destroy() end
Ad

Answer this question