Failed to locate player with FindFirstChild

well, on line 5 it should find the player to do the rest of the function but it just gives an error: Workspace.Arboles.Arbol v4.Tronco.Script:5: attempt to index nil with 'Name'

script.Parent.Parent.EnabledAxe.TouchEnded:Connect(function(hit, player)
	if hit.Parent:FindFirstChild("Hacha") then
		hit.Parent:FindFirstChild("Hacha"):FindFirstChild("TalarArbolAnim").Value = false
	end
	local PlayerId = game.Players:FindFirstChild(player.Name)
	local Backpack = PlayerId:WaitForChild("Backpack")
	if Backpack then
		local Axe = Backpack:FindFirstChild("Hacha")
		if Axe then
			Axe:WaitForChild("TalarArbolAnim").Value = false
		end
	end
end)

TouchEnded does not pass player?
https://developer.roblox.com/en-us/api-reference/event/BasePart/TouchEnded

1 Like

Try replacing the PlayerId variable to this:

local PlayerId = game.Players:GetPlayersFromCharacter(hit.Parent)
1 Like

player is not passed in TouchEnded as @steven4547466 said. Instead, check if hit.Parent gets a player like this:

script.Parent.Parent.EnabledAxe.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Hacha") then
		hit.Parent:FindFirstChild("Hacha"):FindFirstChild("TalarArbolAnim").Value = false
	end
	local player = hit.Parent
	local PlayerId = game.Players:GetPlayerFromCharacter(player)
	if PlayerId then --just a check
		local Backpack = PlayerId:WaitForChild("Backpack")
		if Backpack then
			local Axe = Backpack:FindFirstChild("Hacha")
			if Axe then
				Axe:WaitForChild("TalarArbolAnim").Value = false
			end
		end
	end
end)
2 Likes

In this context I believe that player is not defined.

Player should be hit.Parent which would reference the players character. You can the that players instance in game.Players by doing :GetPlayerFromCharacter()

Reference @Kaid3n22’s post for an example of what I mean.

1 Like