Hey! I’m trying to make a Humanoid move to (And attack) the closest player. I tried making it, but it doesn’t seem to make the Humanoid move. All parts are un anchored. Thanks!
The Code:
local Part = script.Parent
local PathFindingService = game:GetService("PathfindingService")
while wait(1) do
for index, value in ipairs(game:GetService("Players"):GetPlayers()) do
local CharacterAdded = value.Character or value.CharacterAdded:Wait()
local HRP = value.Character.HumanoidRootPart
local RakeModel = Part
local Distance = (HRP.Position - RakeModel.HumanoidRootPart.Position).Magnitude
if Distance <= 100 then
if Distance < Part.CurrentTargetMagnitude.Value then
Part.CurrentTargetMagnitude.Value = Distance
Part.CurrentTargetCharacter.Value = value.Character
local Path = PathFindingService:CreatePath()
Path:ComputeAsync(Part.HumanoidRootPart.Position, Part.CurrentTargetCharacter.Value.HumanoidRootPart.Position)
local PathWaypoints = Path:GetWaypoints()
for index2, waypoint in pairs(PathWaypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Part.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Part.Humanoid:MoveTo(waypoint.Position)
Part.Humanoid.MoveToFinished:Wait()
end
end
end
print(value, Distance)
if Distance > 100 and Part.CurrentTargetCharacter.Value then
Part.CurrentTargetCharacter.Value = nil
Part.CurrentTargetMagnitude.Value = 100
end
end
end
2 Likes
The parts must be all be UNanchored. Click the model, click anchor, and click anchor again and make sure nothing is anchored. Unless you have a part not connected with a weld or motor6d.
Try:
for i = 1,#PathWaypoints do
local waypoint = PathWaypoints[i]
if waypoint.Action == Enum.PathWaypointAction.Jump then
Part.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Part.Humanoid:MoveTo(waypoint.Position)
Part.Humanoid.MoveToFinished:Wait()
end
1 Like
Sort of fixed the issue. Now he just refuses to go after you even tho your in range.
robloxapp-20220117-1143172.wmv (2.9 MB)
Video quality stinks (I don’t have any screen recording software too poor)
I’ve done this before, I used Runservice.Heartbeat and made the character move to the third waypoint of the path and it worked
What do you mean? (CharacterLimit)
so, do you know how the character moves to certain waypoints in a loop? Only move to the third waypoint and use runservice.heartbeat
Can you give a example? I understand things better like that (I’m still 7 months new to scripting)
local self = script.Parent
-- Services --
local Pathfinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
-- Char Parts --
local Root = self:WaitForChild("HumanoidRootPart")
local Hum = self:WaitForChild("Enemy")
-- Enemy Smartness --
local PreyRoot = nil
local Prey = nil
local Dist = nil
-- Tables --
local DebounceTable = {}
-- Connections --
local FollowConnect = nil
-- Callback Functions --
function PathFind(Target)
local Path = Pathfinding:CreatePath()
Path:ComputeAsync(Root.Position,Target.Position)
if Path.Status == Enum.PathStatus.Success then
local waypoint = Path:GetWaypoints()[3] -- No Changing >:(
if waypoint ~= nil then
Hum:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Hum.Jump = true
end
wait()
end
end
end
function FindHighest(Table)
return math.max(table.unpack(Table))
end
-- Loops --
RunService.Heartbeat:Connect(function()
local Objects = workspace:GetChildren()
local Characters = {}
local Distances = {}
for i = 1,#Objects do
local Obj = Objects[i]
if Obj:FindFirstChild("Humanoid") then
if Obj.Humanoid.Health > 0 then
local PreyRoot = Obj.PrimaryPart
local Dist = (Root.Position-PreyRoot.Position).magnitude
if #Characters == 0 and #Distances == 0 then
table.insert(Characters,table.getn(Characters)+1,Obj)
table.insert(Distances,table.getn(Distances)+1,Dist)
else
table.insert(Characters,table.getn(Characters),Obj)
table.insert(Distances,table.getn(Distances),Dist)
end
end
end
end
for i = 1,#Distances-1 do
local Int = FindHighest(Distances)
table.remove(Characters,table.find(Distances,Int)) -- Do not delete the distances thing here it may break this please :(
table.remove(Distances,table.find(Distances,Int))
end
if Characters[1] ~= nil then
local Char = Characters[1]
PreyRoot = Char.PrimaryPart
PathFind(PreyRoot)
Characters = {}
Distances = {}
end
end)
-- Event Functions --
local Debounce = false
Hum.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and Debounce == false and Hum.Health > 0 then
Debounce = true
local Humanoid = hit.Parent.Humanoid
Humanoid:TakeDamage(10)
wait(1.5)
Debounce = false
end
end)
Hum.Died:Connect(function()
Debris:AddItem(self,5)
end)
As you can see in here, it moves to the third waypoint in a path constantly, making it follow the player very greatly, even if the player moves.
So is this a entire script or does this go somewhere in my script? I believe it goes in my script.
Thank you so much, tho.
This is an entire script for enemy pathfinding that I made
1 Like
Is this possible to go in mine? (Char)
Just change the humanoid from enemy to whatever the name of the humanoid in your enemy is and yeah.
What lines are these are? And where would it go? I’m sorry if I’m sounding stupid. I’m still learning.
It’s a whole enemy pathfinding script, you can learn from it and make it similar or replace your script with this, if you want to see how to use it, look in the pathfinding function, it shows how it moves only to the third waypoint.
1 Like
I can’t really understand it. I can’t use it if it’s too advanced to even read for me.
Hello? Are you still able to help me fix the issue?
Still can’t seem to find a solution.
I’m not sure if there’s any more I can help with