Attempt to index nil with value

Hello, I am making a simple AFK checker for my game but I have come across this issue while making it work. Error: attempt to index nil with ‘Value’. This is a server script in ServerScriptService. It occurs on char:FindFirstChild("afkTag).Value. Can someone help? Thanks. Here is my current code:

for i,player in pairs(plrs) do
		if player then
			local char = player.Character
			if char then
				if char:FindFirstChild("afkTag").Value == false then
					char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
					table.remove(AvailableSpawns,1)

					local GameTag = Instance.new("BoolValue")
					GameTag.Name = "GameTag"
					GameTag.Parent = char
				end
			end
		else
			if not player then
				table.remove(plrs,i)
			end
		end
	end

And here is where it gets created: (local script inside starterplayerscripts)

local plr = game.Players.LocalPlayer
local afkTag = Instance.new("BoolValue")
afkTag.Value = false
afkTag.Parent = plr.Character
afkTag.Name = "afkTag"

EDIT: I have just realized that afkTag is not creating. Disregard the first script, does anyone know why the local script doesn’t instance.new()?

If you create the tag on the client then the server can’t see it and can’t read the value of it. Creating the tag on the server should fix this.

1 Like

I have done this but it doesn’t seem to be instance.new()-ing either:

game.Players.PlayerAdded:Connect(function(plr)
	local afkTag = Instance.new("BoolValue")
	afkTag.Value = false
	afkTag.Parent = plr.Character
	afkTag.Name = "afkTag"
end)

You should wait for the player’s character to spawn in before you do anything with it, also it’s good practice to define all the properties of an instance before parenting it to something.

game:GetService("Players").PlayerAdded:Connect(function(newPlayer)
    local character = newPlayer.Character or newPlayer.CharacterAdded:Wait()
    local afkTag = Instance.new("BoolValue")
    afkTag.Name = "afkTag"
    afkTag.Value = false
    afkTag.Parent = character
end)
1 Like

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