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

Why am I getting this nil value error?

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

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.

1 answer

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.

2 comments

0

There is no error now, but the model doesn't get cloned into the workspace, nor do the print functions run

0

That usually means WaitForChild can't find what you're looking for. Check your parenting to see if you've made any errors.

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