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

Problem with my Zombie Follow code?

I get an error when 2 people are in the same server. I get the error "Workspace.Zombie.Follow:6: attempt to index global 'Torso' (a nil value)". I think I understand why, but I don't know how to fix it. Any help?

while wait() do
	local Humanoid = script.Parent.Humanoid
	for i, v in pairs(game.Players:GetPlayers()) do
		Torso = v.Character:WaitForChild("Torso")
	end
	Humanoid:MoveTo(Torso.Position)
end

The script just moves the zombie to the players torso. It's located in the "Zombie" model in workspace.

1 answer

✓ Accepted answer

To prevent the Zombie from changing targets why not check to see which Character is more closer to the Zombie?

local PreviousDistance = 100--Max Distance
local Target
while wait() do
    local Humanoid = script.Parent
 for i, v in pairs(game.Players:GetPlayers()) do
    	local Torso = Torso = v.Character:FindFirstChild("Torso")
	if Torso then 
	local Distance = ( v.Character.Torso.Position - script.Parent.Torso.Position)
		if Distance < PreviousDistance then--Check to see if Distance is smaller than PreviousDistance
			Target = v.Character:FindFirstChild("Humanoid")
			PreviousDistance = Distance
		end
	end
    end
    Target:MoveTo(Torso.Position)
end

Sorry if this script doesn't work! I'll try and fix it after I come back from college tomorrow!

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