Attempt to index rainbow with nil, when it is not nil

The script is OverheadGUI and this error is breaking the script
It works for me (I own the gamepass) but when the second person who does not own the gamepass joins, the error happens and they don’t get an overhead gui

image

local nametag = script:WaitForChild("Rank")
local groupId = 2752852
local MP = game:GetService("MarketplaceService")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local head = char:WaitForChild("Head")
		local guiClone = nametag:Clone()
		guiClone.Parent = head
		guiClone.Tag.Text = plr.Name
		guiClone.Rank.Text = plr:GetRoleInGroup(groupId)
		
		if not MP:UserOwnsGamePassAsync(plr.UserId, 14334251) and plr.Name ~= "NeonSignalYT" then
			guiClone:FindFirstChild("Tag").Rainbow.Disabled = true ( LINE 14 )
			guiClone:FindFirstChild("Tag").TextStrokeTransparency = 0.5
			guiClone:FindFirstChild("Tag").TextColor3 = Color3.new(1, 1, 1)
		else 
			guiClone:FindFirstChild("Tag").Rainbow.Disabled = false
			guiClone:FindFirstChild("Tag").TextStrokeTransparency = 1
			guiClone:FindFirstChild("Tag").TextColor3 = Color3.new(255, 255, 255)
		end
	end)
end)

FindFirstChild() finds the first child with the name, and if it is not there it is nil. So it must not exist when the script runs.

If you are calling on the object so much, you should put it into a variable like cloneTag = guiClone:FindFirstChild("Tag") or use WaitForChild() with a timeout in case it is running too quickly.

Despite what you do, you need a nil check just in case something isn’t cloned properly.

Yes, I see that issue. When I use wait for child it is an infinite wait. I know what the issue is, I don’t know why it is happening though. You can see in the screenshots it exists; which is why I’m confused.

you parented which can caused all the children to be reparented then you tried to access them below that
try this below and see if it fixes the issue

local nametag = script:WaitForChild("Rank")
local groupId = 2752852
local MP = game:GetService("MarketplaceService")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local head = char:WaitForChild("Head")
		local guiClone = nametag:Clone()
		guiClone.Tag.Text = plr.Name
		guiClone.Rank.Text = plr:GetRoleInGroup(groupId)
			
		if not MP:UserOwnsGamePassAsync(plr.UserId, 14334251) and plr.Name ~= "NeonSignalYT" then
			guiClone.Tag.Rainbow.Disabled = true
			guiClone.Tag.TextStrokeTransparency = 0.5
			guiClone.Tag.TextColor3 = Color3.new(1, 1, 1)
		else 
			guiClone.Tag.Rainbow.Disabled = false
			guiClone.Tag.TextStrokeTransparency = 1
			guiClone.Tag.TextColor3 = Color3.new(255, 255, 255)
		end
		guiClone.Parent = head   -- issue was probably here   -- parent after you set everything up
	end)
end)

Also will say if this script doesn’t run fast enough to catch the first character add it won’t setup this tag or anything in the function you should create a SetupCharacter function and call it on initial run and then call it in the character add

1 Like