Need help with subtracting 3% of a value to make it smaller every time a function runs

I have a function for calculating damage I’m currently working on implementing a system where every hit the players deals damage. But it subtracts by 3% of the total value every time the function is ran. Currently I’m out of idea’s for it, so I came here in search of advice. Any help would be appreciated, The function code is below.

This block of code is a dictionary of values for damage.



local Properties = {
	["M1"] = {
		["Debounce"] = false,
		["Combo"] = {["Current"] = "", ["Maximum"] = 100000},
		["Cooldown"] = {["Boolean"] = false, ["Time"] = 1},
		["Timers"] = {["Previous"] = 0, ["New"] = 0, ["Maximum"] = 2},
		["Damage"] = {["Standard"] = 7, ["Critical"] = {["Damage"] = 13, ["Chance"] = 15}}
	}

This is the function itself, AKA the main issue. Again any help is appreciated.

function Damage_Calculation()
local Total = 0
	local Diaposone = rand(0, Properties["M1"]["Damage"]["Critical"]["Chance"])
	
	if Diaposone == Properties["M1"]["Damage"]["Critical"]["Chance"] then
		Total = Properties["M1"]["Damage"]["Critical"]["Damage"]
	else
		Total = Properties["M1"]["Damage"]["Standard"]
	end
	if PreTotal == nil then
		
		Total = Total - Total * 0.03
		PreTotal = Total
	else
		Total = PreTotal - PreTotal * 0.03
	end
	return Total
end

I would say the problem is more of the PreTotal never being set after the first time. You will also want another variable that you can freely change (ex: LastDamage).

local LastDamage

function Damage_Calculation()
	local Total = 0
	local Diaposone = rand(0, Properties["M1"]["Damage"]["Critical"]["Chance"])
	
	if Diaposone == Properties["M1"]["Damage"]["Critical"]["Chance"] then
		Total = Properties["M1"]["Damage"]["Critical"]["Damage"]
	else
		Total = Properties["M1"]["Damage"]["Standard"]
	end
	
	if PreTotal == nil then
		PreTotal = Total
		LastDamage = PreTotal
	else
		LastDamage -= PreTotal*.03
		Total = LastDamage
	end
	return Total
end

Basically, I change the code so the first time it would deal 100% of the damage the first time and would continue going down each time the function is used.

Edit: Scratch that, just realized you don’t even need the second variable, can just do it like this I believe:

function Damage_Calculation()
	local Total = 0
	local Diaposone = rand(0, Properties["M1"]["Damage"]["Critical"]["Chance"])
	
	if Diaposone == Properties["M1"]["Damage"]["Critical"]["Chance"] then
		Total = Properties["M1"]["Damage"]["Critical"]["Damage"]
	else
		Total = Properties["M1"]["Damage"]["Standard"]
	end
	
	if PreTotal == nil then
		PreTotal = Total
	else
		PreTotal -= Total*.03
		Total = PreTotal
	end
	return Total
end

Total seems to be set to the max damage each time, so I could just use that variable each time before setting the total again.

1 Like

Thank you, the solution was right in front of me the entire time XD.

1 Like

Haha, we’ve all had many solutions right in front of us that take us a while to figure out.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.