What do you want to achieve? I’m doing a pathfind with the Module SimplePath, i got the NPC to track player and depending on the distance i wanna change walkspeed, for example if the player is at the distance 1000 then the walkspeed will be 40 but if player is at distance 100 then walkspeed will be 25
What is the issue? It doesn’t work, it’s stuck at the speed of NPCStats.Speed.Walk but pathfind works fine
What solutions have you tried so far? Nothing found in devforum, i tried fixing it myself but i cant, its been hours now
serverscript
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local Assets = ServerStorage.Assets
local CharacterAssets = Assets.Characters
local EntitiesAssets = CharacterAssets.Entities
local NPCFolder = EntitiesAssets:FindFirstChild("Dummy")
if not NPCFolder then
warn("No NPC folder found in ", EntitiesAssets:GetFullName())
return
end
local NPC = NPCFolder:FindFirstChildOfClass("Model")
if not NPC then
warn("No NPC model found in ", NPCFolder:GetFullName())
return
end
local newNPC = nil
local currentTarget = nil
local currentSpeed = nil
local Modules = ServerStorage.Modules
local SContent = ServerStorage.Content
local CharacterContent = SContent.Characters
local EntitiesContent = CharacterContent.Entities
local NPCStatFolder = EntitiesContent:FindFirstChild("Dummy")
if not NPCStatFolder then
warn("No NPC stat folder found in ", EntitiesContent:GetFullName())
return
end
local NPCStat = require(NPCStatFolder.Stats)
local SimplePath = require(Modules.SimplePath)
function UpdateWalkSpeed()
if newNPC then
local humanoid = newNPC:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.WalkSpeed ~= currentSpeed then
if currentSpeed ~= nil then
humanoid.WalkSpeed = currentSpeed
end
end
end
end
function SetupModel()
if not NPC.PrimaryPart then
warn("No PrimaryPart set on NPC model", NPC:GetFullName())
return
end
local NPCHumanoid = NPC:FindFirstChildOfClass("Humanoid")
if not NPCHumanoid then
warn("No Humanoid found on NPC model", NPC:GetFullName())
return
end
local Animator = NPCHumanoid:FindFirstChildOfClass("Animator")
if not Animator then
warn("Animator is missing in:", NPCHumanoid:GetFullName())
return
end
NPCHumanoid.MaxHealth = NPCStat.Health
NPCHumanoid.Health = NPCStat.Health
NPCHumanoid.WalkSpeed = NPCStat.Speed.Wander
NPCHumanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
NPCHumanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
for _, object in NPC:GetDescendants() do
if object:IsA("BasePart") and object.Name ~= NPC.PrimaryPart.Name then
object.CanCollide = false
object.CanTouch = false
object.CanQuery = true
end
end
task.defer(function()
local success, err = pcall(function()
newNPC = NPC:Clone()
newNPC.Parent = workspace.Characters.Entities
newNPC.PrimaryPart:SetNetworkOwner(nil)
end)
if not success then
warn("Failed to setup cloned NPC:", err)
end
currentSpeed = newNPC.Humanoid.WalkSpeed
newNPC.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
currentSpeed = newNPC.Humanoid.WalkSpeed
end)
end)
end
SetupModel()
function TrackPlayer()
local nearestPlayer = nil
local nearestDistance = math.huge
local distances = {
Global = NPCStat.Distances.Detection.Global,
ChargeTo = NPCStat.Distances.Detection.RunRange
}
for _, player in pairs(Players:GetPlayers()) do
local playerCharacter = player.Character
local playerPrimaryPart = playerCharacter and playerCharacter.PrimaryPart
if playerPrimaryPart then
local distance = (newNPC.PrimaryPart.Position - playerPrimaryPart.Position).Magnitude
if distance < distances.Global and distance < nearestDistance then
nearestPlayer = player
nearestDistance = distance
currentTarget = nearestPlayer
print(distance)
end
end
end
if nearestPlayer then
if nearestDistance <= distances.ChargeTo then
currentSpeed = NPCStat.Speed.Run
elseif nearestDistance <= distances.Global then
currentSpeed = NPCStat.Speed.Walk
end
else
currentSpeed = NPCStat.Speed.Wander
end
UpdateWalkSpeed()
return nearestPlayer
end
function InitializePathfind()
repeat
RunService.Heartbeat:Wait()
until newNPC ~= nil
local Path = SimplePath.new(
newNPC, {
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
})
Path.Visualize = true
local targetPlayer = nil
repeat
targetPlayer = TrackPlayer()
RunService.Heartbeat:Wait()
until targetPlayer
local goal = targetPlayer.Character.PrimaryPart
Path.Blocked:Connect(function()
Path:Run(goal)
end)
Path.WaypointReached:Connect(function()
Path:Run(goal)
end)
Path.Reached:Connect(function()
Path:Run(goal)
end)
Path.Error:Connect(function(errorType)
Path:Run(goal)
end)
Path:Run(goal)
end
InitializePathfind()
RunService.Heartbeat:Connect(function()
if newNPC then
print("Variable speed", currentSpeed)
print("WalkSpeed", newNPC.Humanoid.WalkSpeed)
end
end)
i try to update walkspeed in the function trackplayer, idk if im dumb or something cuz i cant find the issue
Wouldn’t this keep resetting the NPC’s walk-speed back to its default value? Seems unnecessary for me, since you’re updating the NPC’s walk-speed in the TrackPlayer function, rather than changing it using the Humanoid’s WalkSpeed property
yes, its detecting players correctly, both are printing the same speed so neither are updating, default speed is 8 but when player gets in range it changes to 12, i expect it to change to 18 when closer but its stuck at 12
I made some changes to your script, hopefully it will work now:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local Assets = ServerStorage.Assets
local CharacterAssets = Assets.Characters
local EntitiesAssets = CharacterAssets.Entities
local NPCFolder = EntitiesAssets:FindFirstChild("Dummy")
if not NPCFolder then
warn("No NPC folder found in ", EntitiesAssets:GetFullName())
return
end
local NPC = NPCFolder:FindFirstChildOfClass("Model")
if not NPC then
warn("No NPC model found in ", NPCFolder:GetFullName())
return
end
local newNPC = nil
local currentTarget = nil
local currentSpeed = nil
local Modules = ServerStorage.Modules
local SContent = ServerStorage.Content
local CharacterContent = SContent.Characters
local EntitiesContent = CharacterContent.Entities
local NPCStatFolder = EntitiesContent:FindFirstChild("Dummy")
if not NPCStatFolder then
warn("No NPC stat folder found in ", EntitiesContent:GetFullName())
return
end
local NPCStat = require(NPCStatFolder.Stats)
local SimplePath = require(Modules.SimplePath)
function UpdateWalkSpeed(newWalkSpeed) -- No longer need to worry about currentSpeed being nil
if newNPC then
local humanoid = newNPC:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.WalkSpeed = newWalkSpeed
currentSpeed = newWalkSpeed
end
end
end
function SetupModel()
if not NPC.PrimaryPart then
warn("No PrimaryPart set on NPC model", NPC:GetFullName())
return
end
local NPCHumanoid = NPC:FindFirstChildOfClass("Humanoid")
if not NPCHumanoid then
warn("No Humanoid found on NPC model", NPC:GetFullName())
return
end
local Animator = NPCHumanoid:FindFirstChildOfClass("Animator")
if not Animator then
warn("Animator is missing in:", NPCHumanoid:GetFullName())
return
end
NPCHumanoid.MaxHealth = NPCStat.Health
NPCHumanoid.Health = NPCStat.Health
NPCHumanoid.WalkSpeed = NPCStat.Speed.Wander
NPCHumanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
NPCHumanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
for _, object in NPC:GetDescendants() do
if object:IsA("BasePart") and object.Name ~= NPC.PrimaryPart.Name then
object.CanCollide = false
object.CanTouch = false
object.CanQuery = true
end
end
task.defer(function()
local success, err = pcall(function()
newNPC = NPC:Clone()
newNPC.Parent = workspace.Characters.Entities
newNPC.PrimaryPart:SetNetworkOwner(nil)
end)
if not success then
warn("Failed to setup cloned NPC:", err)
end
currentSpeed = newNPC.Humanoid.WalkSpeed
end)
end
SetupModel()
function TrackPlayer()
local nearestPlayer = nil
local nearestDistance = math.huge
local distances = {
Global = NPCStat.Distances.Detection.Global,
ChargeTo = NPCStat.Distances.Detection.RunRange
}
for _, player in pairs(Players:GetPlayers()) do
local playerCharacter = player.Character
local playerPrimaryPart = playerCharacter and playerCharacter.PrimaryPart
if playerPrimaryPart then
local distance = (newNPC.PrimaryPart.Position - playerPrimaryPart.Position).Magnitude
if distance <= distances.Global and distance < nearestDistance then
nearestPlayer = player
nearestDistance = distance
currentTarget = nearestPlayer
print(distance)
end
end
end
if nearestPlayer then
if nearestDistance <= distances.ChargeTo then
UpdateWalkSpeed(NPCStat.Speed.Run)
elseif nearestDistance <= distances.Global then
UpdateWalkSpeed(NPCStat.Speed.Walk)
end
else
UpdateWalkSpeed(NPCStat.Speed.Wander)
end
return nearestPlayer
end
function InitializePathfind()
repeat
RunService.Heartbeat:Wait()
until newNPC ~= nil
local Path = SimplePath.new(
newNPC, {
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
})
Path.Visualize = true
local targetPlayer = nil
repeat
targetPlayer = TrackPlayer()
RunService.Heartbeat:Wait()
until targetPlayer
local goal = targetPlayer.Character.PrimaryPart
Path.Blocked:Connect(function()
Path:Run(goal)
end)
Path.WaypointReached:Connect(function()
Path:Run(goal)
end)
Path.Reached:Connect(function()
Path:Run(goal)
end)
Path.Error:Connect(function(errorType)
Path:Run(goal)
end)
Path:Run(goal)
end
InitializePathfind()
RunService.Heartbeat:Connect(function()
if newNPC then
print("Variable speed", currentSpeed)
print("WalkSpeed", newNPC.Humanoid.WalkSpeed)
end
end)
still doesnt work, i debugged a little bit and it seems that the print “distance RUN” that i added doesnt print which means uhhh that its not checking if the distance is less than ChargeTo, only the “distance WALK” prints
if nearestPlayer then
if nearestDistance <= distances.ChargeTo then
print("distance RUN") -- doesnt print
UpdateWalkSpeed(NPCStat.Speed.Run)
elseif nearestDistance <= distances.Global then
UpdateWalkSpeed(NPCStat.Speed.Walk)
print("distance WALK") -- this prints
end
else
UpdateWalkSpeed(NPCStat.Speed.Wander)
end
I now suspect that the issue is happening due to the if statements being the opposite of what they’re supposed to be. Since you want the NPC to run if the player is far away:
You’ll need to check if the player is within walking distance first, since you’re using the lesser-than or equal to operator to compare the distances. Try this:
if nearestPlayer then
if nearestDistance <= distances.Global then
print("distance WALK")
UpdateWalkSpeed(NPCStat.Speed.Walk)
elseif nearestDistance <= distances.ChargeTo then
UpdateWalkSpeed(NPCStat.Speed.Run)
print("distance RUN")
end
else
UpdateWalkSpeed(NPCStat.Speed.Wander)
end
If that still doesn’t work, then make sure that you aren’t accidentally mixing up distances.Global with distances.ChargeTo. If distances.ChargeTo is equal to 1000 and distances.Global is 100, then the issue could be happening due to comparing the nearest player’s distance with distances.Global instead of distances.ChargeTo here (the if statement issue I mentioned above will also need to be fixed in this case, as well):