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

Help With 'Find Torso' and certain players?

while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~=nil then
if target.Parent.Name ~= script.Parent.Owner.Value then
script.Parent.Zombie:MoveTo(target.Position, target)
end
end
end

This is part of a Zombie script. I made this script work so that it won't attack the closest player if it's the creator of the zombie. It works well except that it doesn't work the way I need it to and I don't know how to fix it.

Basically, it always locks on the check if the closest player is the owner, and if the closest player is the owner it doesn't attack it. It'll only attack another player as long as that player is closer than the owner. I'd like to fix it so that it'll go for the second closest player if the closest player is the owner. Can someone help me fix it so that it does what it needs to do?

1 comment

0

Can you provide your findNearestTorso function's sourcecode, please?

1 answer

✓ Accepted answer

The problem is in the findNearestTorso function that you didn't post. The owner is the nearest torso, so you're telling it to do nothing if the owner is the nearest torso, not find the nearest torso that isn't the owner. Here's a solution.

while wait(.1)do
	local dist,target=math.huge
	for _,v in pairs(game.Players:GetPlayers())do
		if v.Name~=script.Parent.Owner.Value then
			local success,torso=pcall(function()
				return v.Character.Humanoid.Health>0 and v.Character.Torso
			end)
			if success and torso then
				local d=(torso.Position-script.Parent.Torso.Position).magnitude
				if dist>d then
					dist,target=d,torso
				end
			end
		end
	end
	if target then
		script.Parent.Zombie:MoveTo(target.Position,target)
	end
end

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