As I’ve been coding my AI, I’ve come across a major problem.
This follow function used to work perfectly fine but now, it has terrible, terrible delays. Thing is, nothing has changed from the script? Every time the NPC reaches the player and calls its attack function, it delays the next follow. Yes, I’ve removed the if statements regarding the pose value but it still follows the player and as soon as it reaches the player, it stops before delaying its next move. I’ve been working for a fix to this but to no avail, which is why I thought of bringing it here.
gangster.PrimaryPart:SetNetworkOwner(nil)
local debounce = false
local path = pathfindingservice:CreatePath()
local function FollowPlayer()
local pose = script.Configuration.Pose
local moving = playerhumanoidrootpart.Position
local compute = path:ComputeAsync(humanoidrootpart.Position, moving)
return path
end
runservice.Stepped:Connect(function()
local pose = script.Configuration.Pose
pose:GetPropertyChangedSignal("Value")
if pose.Value == "Attacking" or pose.Value == "Blocking" then
elseif pose.Value == "Follow" then
FollowPlayer()
local path = FollowPlayer()
for _, waypoint in pairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
end
end
end)
Not much I can think of that can create this, but I will look into this further.
First of all, you are not using the pathfinding service correctly. My solution addresses this. You can see the documents on how to use the pathfinding service in the API documents:
Second, please don’t upload video directly to the forums. Use a video service like Vemio or something.
this is not how you would connect to this and you sure don’t want to continue making connections to something in stepped
not sure if it was your intention if so use the below outside of the stepped connection
pose:GetPropertyChangedSignal("Value"):Connect(function()
-- check the pose.Value here
end)
Also from what i see on that script above if you don’t change the
variable to anything else it will still keep going to same humanoid
and or you need to change the
pose.Value after the for loop ends when they reach the last waypoint to something other than follow… right after this end would be where you need to change it back
I’ve fixed it, I’ve simply edited my script to no longer use pathfinding(yes, I know that’s a bad practice but being honest, it didn’t affect it too bad)
function gangsterai.Follow(char)
local pathfindingservice = game:GetService("PathfindingService")
local gangster = script.Parent
local players = game:GetService("Players")
local runservice = game:GetService("RunService")
local humanoid = gangster.Humanoid
local rootpart = gangster.Torso
local player = char
local playerhumanoidrootpart = char.HumanoidRootPart
local targetdistance = 45
local delaynumb = 2.5
local function findclosesplayer()
local playerlist = players:GetPlayers()
local nearestplayertogangster = nil
local distance = nil
local direction = nil
local distancephysics = (playerhumanoidrootpart.Position - rootpart.Position)
for i,v in pairs(playerlist) do
if not nearestplayertogangster then
nearestplayertogangster = player
direction = distancephysics.Unit
distance = distancephysics.Magnitude
elseif distance <= distance then
nearestplayertogangster = player
direction = distancephysics.Unit
end
end
return nearestplayertogangster, distance, direction
end
runservice.Heartbeat:Connect(function()
local nearestplayer, distance, direction = findclosesplayer()
if distance <= targetdistance and distance >= delaynumb then
humanoid:Move(direction)
else
rootpart.CFrame = CFrame.lookAt(rootpart.Position, playerhumanoidrootpart.Position)
humanoid:Move((Vector3.new()))
end
end)