Hello! I’m really having a hard time making a character following the closest player to it, there are 2 reasons:
- Extreme lag (studio almost crashing)
- The character for some reason sometimes does not detect the player, and even being very close to it, it does not detect the player anymore, so maybe it’s the detection distance.
So, I would like to have the script below this text to make it way less laggy and also make it detect the player at a big distance (like 100 studs).
Players = game:GetService("Players")
PathfindingService = game:GetService("PathfindingService")
local closest_player_distance = math.huge
local closest_player_hrp = nil
local path = PathfindingService:CreatePath({
AgentRadius = 5,
AgentHeight = 6,
AgentCanJump = false,
AgentCanClimb = false,
WaypointSpacing = 2
})
local waypoints = {}
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidrootpart = script.Parent:WaitForChild("HumanoidRootPart")
local whileLoopCoroutine : thread = nil
function UpdatePathfinding()
if whileLoopCoroutine ~= nil then
coroutine.close(whileLoopCoroutine)
end
--Source : https://devforum.roblox.com/t/how-to-make-monster-follow-the-player/1554990/4
--I still did edit things :)
for i, player in pairs(Players:GetPlayers()) do
--Check if the player has a character
local character = player.Character
if not character then
return;
end
local hrp = character:FindFirstChild("HumanoidRootPart") --Yes, HRP = HumanoidRootPart
if not hrp then
return;
end
local this_distance = (humanoidrootpart.Position - hrp.Position).Magnitude
if this_distance < closest_player_distance then
closest_player_distance = this_distance
closest_player_hrp = hrp
end
end
path:ComputeAsync(humanoidrootpart.Position, closest_player_hrp.Position) --Info: ComputeAsync(start : Vector3, end : Vector3) : ()
waypoints = path:GetWaypoints()
whileLoopCoroutine = coroutine.create(function()
while true do
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Walk then
humanoid:ChangeState(Enum.HumanoidStateType.Running)
end
humanoid:MoveTo(waypoint.Position, closest_player_hrp)
humanoid.MoveToFinished:Wait()
end
end
end)
coroutine.resume(whileLoopCoroutine)
end
function Init()
while true do
UpdatePathfinding()
wait(0.1)
end
end
Init()
And as you can see in the script, I tried my best to make this topic’s answer #4 work for me, but my skills have limits so it’s not efficient at all.
The topic that I linked above is the best topic I found so far about this, but it’s not working well enough and couldn’t find any better solution sadly :(.
Before replying, here’s how my character works:
PS:
- please don’t make (if possible) a script with hundreds of lines that aren’t very important, because this would make the script easier to edit for me.
For example, don’t to put methods (character:Pathfind()
for example, instead just usePathfind()
) because they complexify the whole script. - Make it with no delay for calculation, because even with the lag, I could see they stop a bit because I put a
wait(0.1)
in thewhile
loop but this was a mistake - Don’t make huge tables for data, the character has a Configuration folder (that is named Configurations). For each of the values in this folder, just describe the value so I can put it how it should work for my character.
If you do not understanding something about my post (which I wouldn’t be surprised about because I suck at explaining), tell me in the answers please.