Attempt to index nil with waitforchild

I was trying to make a vote system that was awesome and ran into a usual problem. “Attempt to index nil wait “WaitForChild””

This is part of my code, this is the part giving an error.

Dmap1.Touched:Connect(function(hit)
		if hit then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr:WaitForChild("Voted").Value ~= Cmap1.Name and plr:WaitForChild("Voted") ~= nil then -- error
				plr:WaitForChild("Voted").Value = Cmap1.Name
				v1.Value += 1
				if v2.Value > 0 then
					v2.Value -= 1
				elseif v3.Value > 0 then
					v3.Value -= 1
				end
			end
		end
	end)

Help appreicated!

1 Like

Your plr is not found by the method you use sometimes.
Make sure to check if the plr exists first before using that local variable for anything else.

Solution:

if plr and plr:WaitForChild("Voted").Value ~= Cmap1.Name and plr:WaitForChild("Voted") ~= nil then -- error

it works great without errors, thank you!

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