I’m trying to make an AI for a monster which chase players.
The problem is that MoveToFinished wait for the character to stop moving, and not for it to reach the destination of Humanoid:MoveTo()
I tried looking on internet and searching in roblox docs but I couldn’t find a good solution.
MoveToFinished wait for the humanoid to stop moving, but in my case, I want to know when he reached the destination of MoveTo().
If i use MoveToFinished, the monster make small break between the end of the path and the start of the new one.
Do someone know a good solution which doesn’t use too much ressources ?
Here’s the script :
local Character = script.Parent
local Humanoid = Character.Humanoid
local Target = workspace.AITarget
local CharacterScale = Character:GetExtentsSize()
--Services :
local PathFindingService = game:GetService("PathfindingService")
local AgentParams = {
AgentRadius = CharacterScale.X / 2,
AgentHeight = CharacterScale.Y,
AgentCanJump = true,
AgentCanClimb = false,
WaypointSpacing = 4,
Costs = {
--[Enum.Material.Grass] = 2, -- Make the agent prefer walking on grass
--[Enum.Material.Sand] = 10 -- Make the agent avoid walking on sand
}
}
local Path = PathFindingService:CreatePath()
local function ComputePath(Goal)
return pcall(function()Path:ComputeAsync(Character.HumanoidRootPart.Position, Target.Position) end)
end
local function Main()
local Succes, Error = ComputePath(CharacterScale)
if not Succes then print("ERROR COMPUTING PATH") task.wait(0.5) return end
local Waypoints = Path:GetWaypoints()
Humanoid:MoveTo(Waypoints[2].Position)
if Waypoints[2].Action == Enum.PathWaypointAction.Jump then Humanoid.Jump = true end
Humanoid.MoveToFinished:Wait()
end
while true do
print("starting again")
Main()
end
It should be fine, but it depends on how many monsters you have in your game. If you only have one, there will be no problem. I say use the solution unless you see a performance hit in your game, in which case come back here.
If i do that, it will just go to the last waypoint directly, I need the rig to wait he reach the waypoint before going to the next waypoint, I don’t want him to stop before going to next waypoint or him going to the last directly
I don’t understand, doing this won’t change anything, no ? I used MoveToFinished:Wait() but it wait for the monster to stop moving, not when he reached his destination. The result is a monster marking a break at each waypoint before going to the next and I don’t want that. I just want to know when He reached the destination given in MoveTo, not when he stopped.
Have a TouchPart moved to the same Position as the Waypoints with a Touched event on the part. Don’t know the cost, but I am currently using it in something I am making.
local RESET_SECONDS = 3 --Time Delay
local TouchPart = script.Parent
TouchPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.Parent.Name == "Monster" then
if not TouchPart:GetAttribute("IsTouched") then -- Check for non-existence of attribute
TouchPart:SetAttribute("IsTouched", true) -- Set attribute to true
game.ServerScriptService.MainScript.BindableEvent:Fire() --Connect to server script
TouchPart.CanTouch = false
task.wait(RESET_SECONDS) -- Wait for reset time duration
TouchPart:SetAttribute("IsTouched", false) -- Remove attribute
end
end
end)