Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

means the med variable is nil and fyi, :connect() is deprecated (use :Connect())

0

so will using :Connected()) fix the issue?

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.