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

How do you tell the distance one brick is away from another brick?

I'm trying to make a zombie that comes after you once you're 50 studs away from it. However I do not know how to check how far away my character is from the zombie. I know there is a method for it, but I do not know that method. Here's what i have so far.

This is a server script inside the zombie.

local zombie = script.Parent

function getNearestTorso()
	local nearestTorso = nil

	for i,v in pairs(game.Player:GetPlayer()) do
		if [distance from plr to zombie] <= 50  then
			if nearestTorso ~= nil then
				if [distance from nearestTorso to zombie] > [distance from plr to zombie] then
					nearestTorso = v.Character.Torso
				end
			else
				nearestTorso = v.Character.Torso
			end			
		end
	end
	return nearestTorso
end

while true do
	wait(3)
	if getNearestTorso() ~= nil then
		local tor = getNearestTorso()
		zombie.Humanoid:MoveTo(tor.Position,tor)
	end
end

2 answers

✓ Accepted answer

Try this

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 50
	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("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

3 comments

0

My code is fine... I just need to know how to get the distance one brick is from another.

0

Never mind... I saw 'maginitude' and did not know what it was, so I looked it up and it turns out it's just what I needed. Thanks.

0

Is there any way I can accept your answer? Or does this site work differently?

I don't see why people keep using this lame free model michael suggested...I created a follow script that is 6 lines long and does exactly the same thing.

Zeptix just use magnitude and then make the zomie go to the player using the MoveTo method like so:

--Example:

if (brick1.position - brick2.position).magnitude <= followDistance then
	MoveTo(blablabla)
end

I suggest you put everything (except for the variables) inside a while loop, the script will be shorter.

2 comments

0

Actually, your script doesn't do the same thing. Your script runs once and checks for any brick within a certain radius. The script michael posted runs continuously and not only checks for bricks within a certain area, but also checks to see if that brick is the closest brick, if that brick is part of a humanoid, and if that humanoid is still alive.

0

I know the script runs only once, it's just an example

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