How would i make a dummy move to a player?

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

3 Likes

This is perfect! Not to try and put you more at work, but how would i push the enemy out of the players way a bit like seen here?

RobloxScreenShot20220729_132742793

So you want the enemy to dodge?

What I want the enemy to do is be limited to how far they go to the player, I wanted to see how I would do that?

The enemy is inside the player, which is not what i want.

for example you can do

if Target then
		Humanoid:MoveTo(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z)
	end

This didn’t quite work. It printed an error, Unable to cast value to Object.

oops

if Target then
		Humanoid:MoveTo(Vector3.new(Target:FindFirstChild("HumanoidRootPart").Position.X - 6,Target:FindFirstChild("HumanoidRootPart").Position.Y,Target:FindFirstChild("HumanoidRootPart").Position.Z))
	end
1 Like

This works nicely.

One more question, would it be possible to get the player to go on the other side?

If i did something like this, would it require the Target function again?

yes thats possible.
can you just change the dummy’s position to the other side?

This is what i tried:

	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.

no not like that, you could for ex create 2 teleport parts in the workspace
for ex :

local Side1Teleport = game.Workspace.Teleport1
HumanoidRootPart.CFrame = Side1Teleport.CFrame

Hey, real quick, do you know why the enemy is laggy while moving to the player? The enemy keeps on freezing, then going, then freezing, then going.

(I wanted to ask the current question for a while, but I was stuck moving the player to one spot everytime, so my fault if im asking this late.)

Here is a video:
robloxapp-20220729-1557025.wmv (1.6 MB)

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
1 Like

I appreciate you trying man, but the enemy just stays still. I wonder why it does that though.

It also stays laggy as well, which is very odd.

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.