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.
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.