Basically I’m trying to make a combat system and instead of it only hitting the closest #1 player or dummy I want it to hit multiple targets, is there anyway how I can do this? function I’m using is below
local function GetClosest()
local Character = LocalPlayer.Character
local HumanoidRootPart = Character and Character:FindFirstChild("HumanoidRootPart")
if not (Character or HumanoidRootPart) then return end
local TargetDistance = math.huge
local Target
for i,v in ipairs(AliveFolder:GetChildren()) do
if v ~= AliveFolder:FindFirstChild(LocalPlayer.Name) and v:FindFirstChild("HumanoidRootPart") then
local TargetHRP = v.HumanoidRootPart
local mag = (HumanoidRootPart.Position - TargetHRP.Position).magnitude
if mag < TargetDistance then
TargetDistance = mag
Target = v
end
end
end
return Target
end
More importantly, if I could only get the closest infront of my player, and not behind
function getclosesttorso()
local torso,mag = nil,0
local Character = LocalPlayer.Character
local HumanoidRootPart = Character and Character:FindFirstChild("HumanoidRootPart")
if not (Character or HumanoidRootPart) then return end
for _,v in ipairs(AliveFolder:GetChildren()) do
if v ~= AliveFolder:FindFirstChild(LocalPlayer.Name) and v:FindFirstChild("HumanoidRootPart") then
local currmag = (HumanoidRootPart.Position-v.HumanoidRootPart).Magnitude
if currmag > mag then
torso = v.HumanoidRootPart
mag = currmag
end
end
end
return torso,mag
end
There’s no issue, as of right now I have nobody to test with so I have 2 dummies kept in the alive folder. When players join their characters are parented under the folder
Yeah, that’s what the current script I posted is doing. just want to know a way in which I can detect multiple ithat are close rather than just getting one.
I also tried your code and I got an error, but It does the exact same thing as the function I posted so it’s alright.
local function GetClosest()
-- how many studs can the player hit in front
local range = 5
-- divide the range by 2
range /= 2
-- get the players position
local position = LocalPlayer.Character.PrimaryPart.Position
-- move the position in front of the character
position += LocalPlayer.Character.PrimaryPart.CFrame.LookVector * range
-- create a empty table to store the targets
local targets = {}
-- loop all characters alive
for i, character in ipairs(AliveFolder:GetChildren()) do
-- if the character is the same as the local players character then skip
if character == LocalPlayer.Character then continue end
-- if the character is out of range then skip
if (character.PrimaryPart.Position - position).magnitude > range then continue end
-- add the character to the target table
table.insert(targets, character)
end
-- return the targets back to the caller
return targets
end