so, im trying to make a street fighter game, and i want to know how i would make the enemy move to the player and its certain position, any ideas on how i would accomplish this?
ive tried a couple of things such as MoveTo() or Move() but it seems to not be working for me (it would always glitch out and not smoothly move the enemy). experimenting with these functions were quite difficult, and i really dont understand them either.
this is one of the scripts i used to move the enemy to the player. (its very buggy.)
local part = workspace.Part
local dummy = workspace.Enemy
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character or player.CharacterAdded:Wait()
wait(5)
--MoveTo makes the dummy walk to the position you put in args
dummy.Humanoid:MoveTo(char:WaitForChild('HumanoidRootPart').Position)
I cannot find any solutions, some assistance would be appreciated.
Maybe you can use this script put in dummy server script:
The script will move the dummy to the closest player.
local Players = game:GetService("Players")
local Model = script.Parent
local HumanoidRootPart = Model:WaitForChild("HumanoidRootPart")
local Humanoid = Model:WaitForChild("Humanoid")
function findTarget()
local MaxDistance = 100000 -- change the distance in studs that the dummy can see the enemy
local NearestTarget
local Distance
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") then
if Players:GetPlayerFromCharacter(v) then
if v:FindFirstChild("HumanoidRootPart") then
local Hrp = v:FindFirstChild("HumanoidRootPart")
Distance = math.floor((Hrp.Position - HumanoidRootPart.Position).Magnitude)
if Distance <= MaxDistance then
MaxDistance = Distance
NearestTarget = v
end
end
end
end
end
return NearestTarget
end
while true do
local Target = findTarget()
if Target then
Humanoid:MoveTo(Target:FindFirstChild("HumanoidRootPart").Position)
end
wait()
end
if Target then
Humanoid:MoveTo(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z)
end
if Target then
Humanoid:MoveTo(Vector3.new(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z))
end
if Target and Target:WaitForChild('HumanoidRootPart').Position.X then
Humanoid:MoveTo(Vector3.new(Target:FindFirstChild("HumanoidRootPart").Position.X + 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z))
else
Humanoid:MoveTo(Vector3.new(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z))
end
Unfortunately, this didnt do the trick. So I was confused on what i was doing wrong.
And then I got this error saying Workspace.Enemy.Moving:40: attempt to index nil with 'FindFirstChild'.
Note, I am kind of bad at programming so i apologize if im asking for lots of help.
I looked at some forums about NetworkOwnership and its supposedly supposed to make the MoveTo function smoother, but i dont know how to use it. Any help?
MoveToFinished Would Work.
it will wait until the dummy reaches into the target point and if so it will loop again
if Target then
Humanoid:MoveTo(Vector3.new(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z))
Humanoid.MoveToFinished:Wait()
end
Ok so, ive found out the solution on why this was laggy, one of my scripts in the game for the enemy were making it impossible for the MoveTo function to work. Now all I have to do is fix the enemy script and make sure that this doesnt happen again.
I will make another post on how I would fix this too later on tomorrow.