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

Why am I getting this nil value error?

Asked by
AshJack 15
9 years ago

I am trying to make a script that picks a random model, clones it, and changes the parent so that it isn't picked again, however, I am receiving an error that I do not know how to fix:

20:00:42.317 - Players.Player.PlayerGui.ScreenGui.Buy New Floor.TextButton:3: attempt to index a nil value 20:00:42.317 - Stack Begin 20:00:42.318 - Script 'Players.Player.PlayerGui.ScreenGui.Buy New Floor.TextButton', Line 3 20:00:42.318 - Stack End

Here is my code (Normal Script, not local. Also, UnbuiltFloors and BuildFloors are created in another script that is fully functional. They are models.):

reception = game.Workspace.Lobby.Spawn

local list = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("UnbuiltFloors"):GetChildren()
local m = list[math.random(#list)]

script.Parent.MouseButton1Click:connect(function()


print(m)
print(#list)

    list = script.Parent.Parent.Parent.Parent.Parent.UnbuiltFloors:GetChildren()
    m = list[math.random(#list)]    

    if m.Name ~= "" then
        local room = game.ServerStorage:FindFirstChild(m.Name):Clone()
        room.Parent = workspace
        room:MoveTo(reception.Position)

        game.ServerStorage:FindFirstChild(m.Name).Parent = script.Parent.Parent.Parent.Parent.Parent.BuiltFloors

    end



end)
0
Maybe you don't have the right number of .Parents or you don't have enough. Try just getting it from game.Workspace or wherever it is starting with game. And, as in the answer below, use :WaitForChild(). Especially if you're relying on another script to make it. Tkdriverx 514 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

FindFirstChild will return nil if the model is not found. Try using WaitForChild in place of all the instances where FindFirstChild is found in the script.

0
There is no error now, but the model doesn't get cloned into the workspace, nor do the print functions run AshJack 15 — 9y
0
That usually means WaitForChild can't find what you're looking for. Check your parenting to see if you've made any errors. Spongocardo 1991 — 9y
Ad

Answer this question