Hello, I am currently trying to figure out the best way to check if a player is near an explosion that went off with them near it, and if so log the parts that were closest to that explosion. What do you believe is the best method to check this?
You can make a function that first compares the position of all the players in whether they are inside or outside the explosion, if they are inside, it will check all the parts and get the closest one
-- Target player, explosion center
function Check(Player:Player, Center:Vector3)
if Player.Character and Player:DistanceFromCharacter(Center) <= 200 then --200 = Distance
local Magnitudes = {}
for _, Part:BasePart in pairs(Player.Character:GetDescendants()) do
if Part:IsA("BasePart") then
table.insert(Magnitudes, {Part, (Center - Part.Position).Magnitude})
end
end
table.sort(Magnitudes, function(a, b)
return a[2] < b[2]
end)
return Magnitudes[1]
end
end
example to call it
Check(game:GetService("Players")[Player name], Vector3.new(0,0,0))
if they are all, use a loop to analyze all players, if it returns nil it means that it is not inside the explosion
Sources:
1 Like
There’s a built-in “Instance” type for this which covers all of these behaviors: