NPC moving to zero vector (expected NPC to chase player)

Hi i was figuring how to use SimplePath for my game (which is made for gamejam) recently i figured it out how to get player character from server which for some reason SimplePath is not compatible with client however while structuring my script here, anything seems to run fine until all of a sudden

NPC moves to the middle which is where player spawned if there is no spawn location.

local playerSV = game:GetService("Players")
local player_lis = playerSV.LocalPlayer
local replicatserver = game:GetService("ServerStorage")
local directory_walker = require(replicatserver.SimplePath)
local NPC = script.Parent
local HumanoidN = NPC:WaitForChild("Humanoid")
local runSV = game:GetService("RunService")



--local charWORK = workspace.Player
--local char = playerSV:GetPlayerFromCharacter(charWORK) 
local path_dir = directory_walker.new(NPC)
path_dir.Visualize = true


local function trak()
	playerSV.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(charr)
			local humanP = charr:WaitForChild("HumanoidRootPart")
			path_dir:Run(humanP)
		end)
	end)
end


runSV.Heartbeat:Connect(function()
	trak()
end)
1 Like

path_dir:Run() is called when the player is respawned, so it goes to the player’s spawn location and stops because it is not given any new orders when the player moves.

( I don’t think the heartbeat works because the :Run() is binded inside the playeradded and characteradded functions)

2 Likes

Woah, why are you connecting events every heartbeat? Did you mean to iterate all players in the game?

1 Like

Is there another way to get players character with only server-sided script? I’m hoping to make it chase player, calling Character seems to stop npc moving.

On documentation for SimplePath i once tried to loop paths for NPC because it’s not localscript, usually it’s said that it doesn’t require wait() but eventually crashed after testing it so i have to use RunService for better looping (prevents exhausted and crashing)

Not player but rather a character that player is controlling on.

I think you meant to use ReplicatedStorage? You can’t use ServerStorage on the client.

I wasn’t trying to place the module in ReplicatedStorage either as it’s made for server-side script only.

Figured what the problem is, i managed to get this solved by looping the player character in a new way (at first thought it was simplepath bug) turns out that GetPlayers() solved the issue where Character is unable to be indexed.

for anyone looking for source code on how it’s done, i’ve pasted it here so you can add what’s missing

-- for viewing source code purposes

local playerSV = game:GetService("Players")
local replicatserver = game:GetService("ServerStorage")
local directory_walker = require(replicatserver.SimplePath)
local NPC = script.Parent
local HumanoidN = NPC:WaitForChild("Humanoid")
local HumanoidCAP = NPC:WaitForChild("HumanoidRootPart")
local runSV = game:GetService("RunService")


local path_dir = directory_walker.new(NPC)
path_dir.Visualize = true


local function trak()
	local tar = nil
	local player_dir = playerSV:GetPlayers()
	for _,plyr in ipairs(playerSV:GetPlayers()) do
		local char = plyr.Character
		if char then
			local tarG = char:FindFirstChild("HumanoidRootPart")
			if tarG then
				if tar then
					if (HumanoidCAP.Position - tar.Position).Magnitude > (HumanoidCAP.Position - tarG.Position).Magnitude then
						tar = tarG
					end
				else
					tar = tarG
				end
			end
		end
	end
	if tar then
		path_dir:Run(tar.Position)
	end
end

runSV.Stepped:Connect(function()
	trak()
end)

Thanks for explanation @.Nightfall_Intuition and some guy on discord

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