Error on attempt to index nil with 'parents'


What is wrong with my script and how can I fix it? Here is my script below

local button = script.Parent
local player = game.Players.LocalPlayer
local tweenservice = game:GetService("TweenService")
local ws = game:GetService("Workspace")
local screens = ws:WaitForChild("Screens")

button.MouseButton1Click:Connect(function()
	local stuff = screens:GetDescendants()
	for i,v in pairs(stuff) do
		if v:IsA("StringValue") and v.Value == player.Name then
			v.Parent.Visible = false
			v.Parent.Parent.TextButton.Visible = true
			if v.Parent.Parent.Parent.Parent.Name == "Screen1" then
				game.ReplicatedStorage.Changevalue1:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen2" then
				game.ReplicatedStorage.Changevalue2:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen3" then
				game.ReplicatedStorage.Changevalue3:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen4" then
				game.ReplicatedStorage.Changevalue4:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen5" then
				game.ReplicatedStorage.Changevalue5:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen6" then
				game.ReplicatedStorage.Changevalue6:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen7" then
				game.ReplicatedStorage.Changevalue7:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen8" then
				game.ReplicatedStorage.Changevalue8:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen9" then
				game.ReplicatedStorage.Changevalue9:FireServer(v)
			elseif v.Parent.Parent.Parent.Parent.Name == "Screen10" then
				game.ReplicatedStorage.Changevalue10:FireServer(v)
			end
			local tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Bounce)
			local goals = {Position = UDim2.new(0.42, 0,1, 0)}
			local tween = tweenservice:Create(button,tweeninfo,goals)
			tween:Play()
		end
	end
end)
game.ReplicatedStorage.Claimbuttonvisible3.OnClientEvent:Connect(function(v)
	v.Parent.Visible = false
	v.Parent.Parent.TextButton.Visible = true
end)

image
Btw this is how it looks like in the explorer

The error means your code is basically trying to do

nil.Parent

More specifically for your code it means one of your variables is nil. For example, one of the errors means v.Parent.Parent.Parent.Parent is nil.

You shouldn’t loop through all the descendants if you don’t need to. You also shouldn’t use the usernames for searching Instance names, because people can pick usernames that break your game by matching things like SurfaceGui, Visitorscreen10, etc (use userids instead).

Your error is probably coming from doing .Parent until you get to game, which I believe has .Parent as nil. For example, Screen1 is a descendant of Screens, but:

is Screens → Workspace → game → nil, which causes an error if you then do .Name.

But I want to use the player name as a reference as it is used to show in a Screen GUI for other players as I am making a cash register and I need people to know who claimed it. I also didn’t change the name of the string value I just change the value of it instead to indicate that this register is claimed. So what should I do instead because I want to keep the player’s name inside the string value

1 Like

Yep, you’re totally right and I misread that.

I would check the Screen1.Parent.Parent.Parent.Parent.Name thing though.

But like the v is a string value so how should I check it to make sure that it isn’t nil

You can check using a simple line of code.

if not v then continue end

v if nil, returns false. So if v is false, that code runs, which skips the rest of code for that loop.

(continue and break are not the same thing. I kind of explained them poorly.)

Oh so something like repeat… until?

I don’t understand the question. Can you clarify, please?

Nvm I found the problem in my code

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.