You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I am making monster A.I’s catching or attack system.
-
What is the issue? Include screenshots / videos if possible!
I am using :MoveTo() But It Doesn’t Work Smoothly And Stops When Player Changed It’s Position.
JUMPSCARE ALERT IN THIS VIDEO ( Not Much Scary Tho )
local function Attack(target)
local distance = (humanoidrootpart.Position - target.PrimaryPart.Position).Magnitude
if target.Humanoid.Health == 0 then return end
if distance > 2 then
monsterchar.Humanoid.WalkSpeed = 24
humanoid:MoveTo(target.PrimaryPart.Position, target.PrimaryPart)
else
game:GetService('ReplicatedStorage').Events["JumpScare Events"]["Monster-K Event"]:FireClient(players:GetPlayerFromCharacter(target))
monsterchar.Humanoid.WalkSpeed = 0
monsterchar.Humanoid.JumpPower = 0
MonsterKAnimationLoad:Play()
target.Humanoid.Health = 0
MonsterKAnimationLoad.Stopped:Wait()
monsterchar.Humanoid.WalkSpeed = 16
monsterchar.Humanoid.JumpPower = 50
end
end
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I Tried Setting NetworkOwner nil But I Don’t Think That Is The Problem And Tried Inserting Two Arguments Into :MoveTo().
Before - :MoveTo(target.PrimaryPart.Position).
After - :MoveTo(target.PrimaryPart.Position, target.PrimaryPart).
Thanks For Reading Untill Here! Have A Good Day!
1 Like
Setting Ownership is good for physics and stuff like that. It also helps against exploiters. However this looks like a moveTo wait or wait issue. How often you call Attack function? If there is a sec or two it will wait to move again till. Unless moveTo will not fire again after already called. I’m not sure really.
Basically update the targets position more to make the AI move more without stopping, or try using the Pathfinding service.
2 Likes
It Patrols Every 0.5 Seconds And When It Got The Target It Breaks The Loop.
1 Like
So It Is Just The Function Itself. Should I Put The Function In A Loop Or No?
I would put it in a loop. Have u tried wait faster than 0.5? I don’t want the game to lag or anything, but there may be a solution of getting the players position plus speed to basically lock on the player.
Anyways usually fire ever 0.1 sec or lower to follow player for smoother follow.
1 Like
The code is like this if i can’t describe perfectly :
local function Attack(target)
local distance = (humanoidrootpart.Position - target.PrimaryPart.Position).Magnitude
if target.Humanoid.Health == 0 then return end
if distance > 2 then
humanoid.WalkSpeed = 24
humanoid:MoveTo(target.PrimaryPart.Position, target.PrimaryPart)
else
game:GetService('ReplicatedStorage').Events["JumpScare Events"]["Monster-K Event"]:FireClient(players:GetPlayerFromCharacter(target))
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
MonsterKAnimationLoad:Play()
target.Humanoid.Health = 0
MonsterKAnimationLoad.Stopped:Wait()
humanoid.WalkSpeed = 16
humanoid.Humanoid.JumpPower = 50
end
end
local function MoveTo(destination)
local path = getPath(destination)
for index, waypoint in pairs(path:GetWaypoints()) do
local target = getTarget()
if target then
Attack(target)
break
else
if humanoid.WalkSpeed == 24 then humanoid.WalkSpeed = 16 end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
local function patrol()
local waypoints = game:GetService('Workspace').Monster.Positions:GetChildren()
local random = math.random(1, #waypoints)
MoveTo(waypoints[random])
end
while wait(0.5) do
patrol()
end
when i do true it just stops every time and comes back and becomes laggier
Interesting. This is code I used for my NPCs It’s bland and basic but it works, and I also use it for hoards of NPCs although it does cause lag at some point. It was ment for a combat experience.
while true do
local target = module.findNearestPlayer(script.Parent.Parent.HumanoidRootPart.Position)
if target ~= nil and script.Parent.Parent:FindFirstChild("Stun") == nil then
if (target.Position - script.Parent.Parent.HumanoidRootPart.Position).magnitude < 10 then
walkAnim:Play()
end
hum:MoveTo(target.Position, target)
walkAnim:Play()
end
end
the Find Target:
function module.findNearestPlayer(Position)
wait(0.3)
local List = game.Workspace:children()
local Torso = nil
local Distance = 25
local Temp = nil
local Human = nil
local Temp2 = nil
for x = 1, #List do
Temp2 = List[x]
if (Temp2.className == "Model") and (Temp2 ~= script.Parent) then
Temp = Temp2:findFirstChild("HumanoidRootPart")
Human = Temp2:findFirstChild("Humanoid")
if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
if (Temp.Position - Position).magnitude < Distance then
Torso = Temp
Distance = (Temp.Position - Position).magnitude
end
end
end
end
return Torso
end
1 Like
Does It Stops When The Target Changes Position Like Mine? If No I Don’t Know What’s Wrong With My Script.
I just ran to test it and it did not stop or pause at all in-fact It was way faster than me and it could not attack me as the code is old and messed up. even though there is a wait 0.3.
1 Like
Yeah… I Don’t Know What’s Wrong With My Script…
let’s look into it. I’ll test it in studio.
1 Like
Ok (35 Characters Limit Yeah…)
what is ’ getTarget() 'may I ask?
1 Like
It gets the nearest target. if you need the code i can send it.
is it like the code I posted where it getsChildren in workspace?
1 Like
it gets players and uses for to loop every nearest player
Anyways appreciate for the help i am gonna do something else because it is late in my timezone.
is it a loop in a loop? that might cause the lag
1 Like
oh sorry, for bothering you at such a time. Have a Blessed Night.
1 Like