I made a (janky) equation to calculate the damage a player should take from an explosion factoring in their distance from the blast and a script to go along with it. Is there any way I can improve this script, as it will be used very frequently in my game.
The script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("test")
remote.OnServerEvent:Connect(function()
for i,v in pairs (game.Players:GetChildren()) do
local hrp = v.Character:WaitForChild("HumanoidRootPart")
local casterpos = game.Workspace.caster.Position
local explosionRay = Ray.new(casterpos, hrp.Position-casterpos)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(explosionRay, {game.Workspace.caster})
if hit then
if hit.Parent == v.Character or hit.Parent.Name == "Frame" or hit.Parent:IsA("Accessory") then
--1 = insta-kill, 2 = larger explosion, 3 = medium explosion, etc
local explosionPower = 5 --the higher the number, the less powerful the explosion is
local radius = 25
if (casterpos - position).magnitude < radius then
local magnitude1 = (casterpos - position).magnitude
local humanoid = v.Character.Humanoid
local damage = (((100 - math.pow(explosionPower, (magnitude1 *.2)))+10)*.7) --pls tell me how I can improve this line, if possible
if damage > 0 then
local roundedDamage = math.ceil(damage)
humanoid:TakeDamage(roundedDamage)
end
end
end
end
end
end)
thank you so much for your help, as for math.ceil, it’s essentially just next largest integer (3.4- 4), so it’s essentially just rounding a number up, no?
I thought it would be easier to just round the number up than to round it “correctly”. Also, how could I make my code more readable via indentation?