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

The running event of Humanoid isn't connecting to my function?

I am making a pathfinding zombie but I need it to update the path when the player it's following moves. However, the event connecting to the function never fires, doesn't give an output, and won't break if I put a breakpoint at that line.

This first part of the code runs fine:

	players = game.Players:GetPlayers()
	randomPlayer = players[math.random(1, #players)]
	print(randomPlayer.name)
	pathService = game:GetService("PathfindingService")

The broken-looking code is here, and it'd be helpful to know what went wrong:

game.Workspace:WaitForChild(randomPlayer).Character.Humanoid.Running:connect(function(speed)
		print("Moved")
		game.Workspace.Points:ClearAllChildren()
		wait(0.1)
		rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
		path = rawPath:GetPointCoordinates()
end)

I will also post the entire script (which isn't much) in case the problem doesn't lie in the above snippet:

wait(0.1)
while true do
	players = game.Players:GetPlayers()
	randomPlayer = players[math.random(1, #players)]
	print(randomPlayer.name)
	pathService = game:GetService("PathfindingService")
	
	wait(0.1)
		
	rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
	path = rawPath:GetPointCoordinates()
	
	
	game.Workspace.Points:ClearAllChildren()
		
	partReached = true
	
	for x = 1, #path do
			
		if partReached == true then
			partReached = false
			p = Instance.new("Part", game.Workspace.Points)
			p.CanCollide = false
			p.FormFactor = Enum.FormFactor.Symmetric
			p.Size = Vector3.new(1,1,1)
			repeat wait() until path[x] ~= nil
			p.Position = path[x]
			p.Anchored = true
			p.BrickColor = BrickColor.DarkGray()
			p.Transparency = 0
			script.Parent.MazeHumanoid:MoveTo(p.Position, p)
		end
			
		while partReached == false do
			wait(0.1)
			p.Touched:connect(function(part)
				if part.Parent.MazeHumanoid ~= nil or part.Parent.Parent.MazeHumanoid ~= nil then
					partReached = true
				end
			end)
		end
		
	end
end

game.Workspace:WaitForChild(randomPlayer).Character.Humanoid.Running:connect(function(speed)
		print("Moved")
		game.Workspace.Points:ClearAllChildren()
		wait(0.1)
		rawPath = pathService:ComputeRawPathAsync(script.Parent.Torso.Position, randomPlayer.Character.HumanoidRootPart.Position, 512)
		path = rawPath:GetPointCoordinates()
end)

1 answer

Well, there are technically two ways to fix this, The problem is, when you use WaitForChild(), you put the player object instead of the player's name. This "confuses" the WaitForChild(), making it think you're looking for the Instance of the Player instead of their character. Now, here are the ways to fix your script:

Either

  1. In the first part, simply add .Name to the end of line 2. (This would require you to have to remove the `.name' at line 3)

Or

  1. Change WaitForChild(randomPlayer) in the second part to WaitForChild(randomPlayer.Name).

There were a few other small things I noticed that may mess the script up, so let me know if it still doesn't work.

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