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)

1 comment
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.