Please tell me how I can improve this script

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)


I don’t know sorry.

Though for already working scripts that you want to be improved you post into coding support.

Fixed it. (30 ch.racter limit is annoying lol.)

The code is a bit hard to read due to the indenting.

local Players = game:GetService("Players")
-- The GetPlayers method is a bit more specific here
for _, player in pairs(Players:GetPlayers()) do

Ceiling a number is not the same as rounding a number.

I recommend to move this bit of functionality to another function called calculateDamage, since this line is not easily readable.

Why WaitForChild instead of FindFirstChild to check if it exists?

1 Like

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?

To round is different than to round up. (3.4 → 3; 3.6 → 4)

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?

to help out

math.floor rounds down
math.ceil rounds up
math.round rounds to the closest whole number

1 Like

My comment was a bit nitpicky. I disagreed with the name of the variable. You can ignore that part if you want.

local ceiledDamage = math.ceil(damage)

it’s alright, I appreciate the help