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

attempt to index nil with 'Parent'????

Help me pls! Error Players.OsipStar4ik.PlayerScripts.NameTagGiver:36: attempt to index nil with 'Parent' Script

local plr = game.Players.LocalPlayer
local vipServerOwnerId = game.ReplicatedStorage.Interactions.Server.GetVIPServerOwnerID:InvokeServer()

local specialColors = {}
specialColors["635035673"] = Color3.fromRGB(255, 221, 0) -- BlockyShadows

function givePlayerNameTag(thisPlr)

	-- Make sure player
	local chr = thisPlr.Character
	if chr ~= nil and thisPlr ~= plr then

		-- Create nametag
		local tag = script.NameTag:clone()

		-- Set information
		tag.Label.Text = thisPlr.Name
		if thisPlr.UserId == vipServerOwnerId then
			tag.Label.Text = "[SERVER OWNER] " .. thisPlr.Name
			tag.Label.TextColor3 = Color3.fromRGB(255, 0, 255)
		elseif thisPlr.UserId == "erer" then
			tag.Label.Text = "[CREATOR] " .. thisPlr.Name
			tag.Label.TextColor3 = Color3.fromRGB(255, 0, 0)
		end
	elseif thisPlr.UserId == "OsipStar4ik" then
		tag.Label.Text = "[DEVELOPER] " .. thisPlr.Name
		tag.Label.TextColor3 = Color3.fromRGB(255, 0, 0)
	end
	for id, c in pairs(specialColors) do
		if tostring(thisPlr.UserId) == id then
			tag.Label.TextColor3 = c
		end
	end

	-- Add to head
	tag.Parent = chr:WaitForChild("Head")

end


game.Players.PlayerAdded:connect(function(thisPlr)

	-- Assign event
	thisPlr.CharacterAdded:connect(function()
		givePlayerNameTag(thisPlr)
	end)
	givePlayerNameTag(thisPlr)

end)

for _, i in pairs(game.Players:GetPlayers()) do
	i.CharacterAdded:connect(function()
		givePlayerNameTag(i)
	end)
	givePlayerNameTag(i)
end

1 comment

0

You're trying to reference a variable that was declared in a different scope.

1 answer

✓ Accepted answer

the variable 'tag' is defined inside the "if chr ~= nil and thisPlr ~= plr then" function, while on line 36 you try to use tag outside the function, which will return an error. try duplicating that line and put one between lines 24 - 25 and the other between 27 - 28

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