Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Projectile targets near by players?

Asked by 6 years ago

What happens is that a ball formed over the players head shoots smaller balls that targets and goes towards the player, thus damaging them. Everything works find and there are no errors, the thing is, it doesn't automatically target a player. It only prints my name as the closest player. Please someone help me fix this Function

function GetClosestTorso()
local closest;
local closestdist;
for i,v in pairs(game.Players:GetPlayers()) do
if v.Character and v.Character.Name ~= Player.Name and v.Character:FindFirstChild("UpperTorso") then
local mag = (Player.Character.UpperTorso.Position - v.Character.UpperTorso.Position).magnitude
local comparison = (closestdist or 1000)
if mag <= comparison then
closest = v.Character.UpperTorso
closestdist  = mag;
end
end
end
return closest
end

Execution

local torso = GetClosestTorso()
if torso then
print(torso.Parent.Name)
-- print the name of the closest character
end
if torso and torso.Parent.Name ~= Player.Name then
        bull.CFrame = CFrame.new(bull.Position,torso.Position)
        end

Answer this question