Hello fellow developers, I’m trying to make an ability that will use magnitude to detect anyone in range of 30 studs and attack them, but only one of them (and closest one if possible), how can I do this? I’m not looking for an entire script maybe like how I could do this and why it does this, thank you!
Loop through all the players and check if their player is close by using player:DistanceFromCharacter()
local function DamagePlayersInRange(pos, range, dmg) for i, v in pairs(game.Players:GetPlayers()) do if v.Character and v:DistanceFromCharacter(pos) < range then v.Character.Humanoid:TakeDamage(dmg) end end end
That doesnt seem to be working for me ;(
Show your code, @dandcx did not provide you a code that works without some tweaks such as changing the damage variable and position, range. Therefore it leads me to believe that you maybe tried to use the code and as such got a surprise.
To get the positions of players, you can either use WorldRoot:GetPartBoundsInRadius or check the players individually to get their positions via magnitude.
To get the closest player in the given radius, you’ll have to do a simple algorithm to find the lowest magnitude of all items in the list.
I’m not sure I recommend using Magnitude here instead of
GetPartBoundsInRadius as Magnitude only uses the center of the hitbox you’re trying to detect, which can lead to objects visually entering range but not being targeted. (Consider a large hitbox whose edge walks into range, it will not be targeted since Magnitude only considers the center)
For an example of a simple algorithm to find the lowest value (Written in PsuedoCode)
distance = MagnitudeOfFirstObjectInList for i, v in pairs(ListOfObjects) do if GetDistance(v) < distance then distance = GetDistance(v) end end
This is my code:
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr) local char = plr.Character local root = char.HumanoidRootPart local pos = root.Position local range = 30 local dmg = 50 local function DamagePlayersInRange(pos, range, dmg) for i, v in pairs(game.Players:GetPlayers()) do if v.Character and v:DistanceFromCharacter(pos) < range then v.Character.Humanoid:TakeDamage(dmg) end end end end)
It didn’t work me but I think it would damage all players in the range not just one (I used the test feature in studio to test out the script)
id make a part that’s 60 studs
center the part on a point
get all parts it touches, placed into a table
loop through the table for humanoidrootparts, place those into a new table
loop through the new table for the distances to each humanoidrootpart, updating a local variable if the distance is closer
damage closest humanoid
but there is probably a better way to do it
region3 may or may not have a specific function to get humanoids, cant remember
looping through player characters works too (and is less tedious than the above method i described, but I don’t use it in case I want non-player humanoids or parts)
Sorry for bumping a several month old post, you could add a debounce to where it damages the player on the server. I did that and it works for me