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
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) Use the hitbox part to wherever you need, like in the grenade function.
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
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