I need help with an "attempt to index nil with ..." problem

I’m trying to script a voting system which involves a boolean variable, onteleporter, to detect when the player is individually standing on the teleporter platform. I’ve been having an issue with “attempt to index nil with ___” and it is causing my script to break. Any help would be greatly appreciated

local teleportpad = script.Parent.teleportpad

game.Players.PlayerAdded:Connect(function(player) --triggers when player join, adds values

local playervalues = Instance.new("Folder")

playervalues.Name = "playervalues"

playervalues.Parent = player

local onteleporter = Instance.new("BoolValue") --value to mark if someone is waiting being teleported

onteleporter.Name = "onteleporter"

onteleporter.Parent = playervalues

onteleporter.Value = false

pad.Touched:Connect(function(hit)

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

player.playervalues.onteleporter.Value = true

end)

end)

An odd feature of this error is that when I test the game to check if onteleporter was turned to true after touching the block, it functioned correctly. The error, however, still breaks everything proceeding the event.

So far, I have tried using WaitForChild to identify the playervalues folder, with little effect, and then again with onteleporter. I have also applied FindFirstChild and have had similar results

1 Like

Try this -

game.Players.PlayerAdded:Connect(function(player) --triggers when player join, adds values
	local playervalues = Instance.new("Folder")
	playervalues.Name = "playervalues"
	playervalues.Parent = player

	local onteleporter = Instance.new("BoolValue") 
	onteleporter.Name = "onteleporter"
	onteleporter.Parent = playervalues
	onteleporter.Value = false
end)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		Player:WaitForChild("playervalues").onteleporter.Value = true
	end
end)

When you’re using Touched event, and you want to only do somethings that only players/npcs can have , you must check if the object that touched that part, has a Humanoid.