Area of Effect Weapon help

I am creating a grenade for my game, however I don’t know a way to distribute the damage to players nearby. I am using a serverscript to prevent exploits but the way I have imagined i don’t think is very performative

grenade()
    for i, v in pairs(game:GetPlayers()) do
         --gets all characters and checks position relative to grenade position
    end
end

if I am wrong and this is completely fine to do please lmk

Hello!
Hope your having a good day because your script ain’t :skull:
Use hitboxes after the charge reaches its explosion point. Here is a slight example where the hitbox is a part that you will have to make, and uses :GetPartsInPart:

local Hitbox = *your hitbox here fr*
local PS = game:GetService("Players")
--Paste this into when you want the explosion to register damage:
local OParams = OverlapParams.new()
OParams.FilterType = Enum.RaycastFilterType.Exclude
OParams.FilterDescendantsInstances = { *UserCharacter Here* }

local Result = workspace:GetPartsInPart(Hitbox, OParams) --OP Params are optional, but def effective!
local HB_Debounce = {}
for i, Part in pairs(Result) do
	if Part.Parent:IsA("Character") and Part.Parent:FindFirstChild("Humanoid") then
		if PS:GetPlayerFromCharacter(Part.Parent) ~= nil then --Identify Player
			--Do Stuff here!
		end
	end
end

I think thats good enough, I hope it works. If it doesn’t, just reply here (I’m lazy to test cuz its night but yuh) :slight_smile: Use the hitbox part to wherever you need, like in the grenade function.

use magnitude

e.g

 grenade()
for i, v in pairs(game:GetPlayers()) do
	if v.Character:WaitForChild("HumanoidRootPart") then
		if (v.Character:WaitForChild("HumanoidRootPart").Position - grenade.Position).Magnitude <= 20 then
			v.Character.Humanoid:TakeDamage(20)
		end
	end
end

just make sure to edit the grenade position

1 Like

Doing a double WaitForChild isn’t the best move, but yeah, that’s what @MrPieFace10 needs.

local PlayerService = game:GetService("Players")

for _, Player in PlayerService:GetPlayers() do
	local Root = Player.Character and Player.Character:FindFirstChild("HumanoidRootPart")
	local Hum = Player.Character and Player.Character:FindFirstChild("Humanoid")

	if Root and Hum and (Root.Position - Grenade.Position).Magnitude <= 20 and Hum.Health > 0 then
		Hum:TakeDamage(20)
	end
end
1 Like

oh, thank you. i dont really know much about these functions :sweat_smile:

1 Like

So yeah there are a lot of options, including:

  • Magnitude
  • Spacial Query
  • Casting
  • etc.

Try em out you will love them all :star_struck: