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)
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.