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

Why does this script not work?

I have this script for a zombie to find the closest person and chase him, but the findNearestTarget() function is not working. there is nothing in the output, please help!!

local player = script.Parent
local followingPath = false
torso = script.Parent.Torso
range = 500
target = nil

function findNearestTarget()
	for _, plr in pairs(game.Players:GetPlayers()) do
		if plr.Character ~= nil then
			tor = plr.Character.Torso
			if target ~= nil then
				if not target or (torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
						target = plr.Character
					break
				end
			end
		end
	end
end

while true do
	wait(.1)
	findNearestTarget()
	if target~=nil then
		script.Parent.tar.Value = Vector3.new(target.Torso.Position)
	end
end

while true do

	if not followingPath then
		followingPath = true
	path = game:GetService("PathfindingService"):ComputeRawPathAsync(player.Torso.Position, script.Parent.tar.Value, 500)
	points = path:GetPointCoordinates()
	
	game.Workspace.Points:ClearAllChildren()
	
	for p = 1, #points do
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = points[p]
		part.Anchored = true
		part.CanCollide = false
		part.Parent = game.Workspace.Points
	end
	
	for _, point in pairs(points) do
		script.Parent.Zombie:MoveTo(point)
		repeat
			distance = (point - player.Torso.Position).magnitude
			wait()
		until distance < 3
	end
	followingPath = false
	end
	wait(0.1)
end


2 answers

Replace Line 12 with this:

if not target or (torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then

2 comments

0

its still not updating tar

0

if you want, I made the model free to get in roblox in my models if you wanna take it and try to figure out whats wrong with it

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