Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Ai in Rpg enemys / distance from spawn?

basically , in my rpg I want it so that if my enemy goes a certain distance from the spawnpoint (I can just use a block) then it walks back to the spawnpoint, so I was wondering if there was a way to measure the stud distance and make it so that it returns to normal spawn.. any help guys ?

if dist >= 10 then
script.Parent.Human:MoveTo(pos) -- dist being the stud distance , and pos being the spawnpoint (e.g)
end

Idk something like that... thanks , any help appreciated

1 answer

✓ Accepted answer
local spawnPoint -- define spawnpoint
local studDistance = 10 -- maximum distance from spawn allowed

while true do
	local dist = (spawnPoint.Position - 
			script.Parent.Torso.Position).magnitude
	if dist >= studDistance then
		script.Parent.Human:MoveTo(
			spawnPoint.Position, spawnPoint)
		wait(1)
	end
end

If you have any questions ask.

2 comments

0

Thanks , this does work, and is helpful , but i just wanted to know why on line 7 you added spawnPoint.Positon and spawnPoint itself, why not one?

0

I believe they removed it by now, however there used to be a second parameter that required you to pass in the object you are moving to or at least a description of it, Habit I guess.

This discussion is preserved as it was. New answers, comments, and votes are closed.