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

URGENT: HOW TO MAKE A MODEL CHASE THE PLAYER CLOSEST?

I know this is going to sound bad... but i know nothing about scripting and I'm trying to make a game. I have a humanoid with no script in him and i'm trying to make him chase the player closest. Anyone that knows how to make the humanoid chase the closest player then please help.

2 comments

0

Don't tag your post as "URGENT". Also, if you know absolutely nothing about scripting, you'll probably need to learn the basics before you attempt a task like this.

0

put a server script in him (or local if you want that for some reason) and make a function that scans for humanoids within a certain magnitude and use the :MoveTo() Function to make him move to the humanoid

1 answer

Script:

function findNearestTorso(pos) -- creates the function with the name findNearestTorso and the pos is the custom parameters for the function
	local list = game.Workspace:GetChildren() -- grabs all the things inside of workspace
	local torso = nil -- prepare a variable
	local dist = 100 -- the distance to activate from
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do -- scan through a table in this case it is named list
		temp2 = list[x] -- use x as the key for the table 
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then -- if the name of the item in the list is a Model and is not the script.Parent then continue script
			temp = temp2:FindFirstChild("HumanoidRootPart") -- search for its HumanoidRootPart
			human = temp2:FindFirstChild("Humanoid") --- Search for a humanoid
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then -- if temp and human are not nil and the humanoid's health is below 0 then continue
				if (temp.Position - pos).magnitude < dist then --check the magnitude and if it is less than the set distance then continue
					torso = temp -- declare the torso to go for
					dist = (temp.Position - pos).magnitude -- declare distance from and to torso
				end
			end
		end
	end
	return torso -- return the torso back to the while true loop when you called on it
end


while true do
	wait(1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) -- call the custom function
	if target ~= nil then
		script.Parent.Zombie:MoveTo(target.Position, target) -- move the humanoid figure to the torso that was fetched from the findNearestTorso() function
	end
end

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