I’m working on a class fighting game and for one of my classes I had an idea to have a passive ability where every kill increases the damage of one of your abilities and the code works, however, I wanted to know what I could do to make it more efficient.
local PlayerKills = {} -- seperate from the CombatHandler and used for the reapers passive ability
-- damage increases the more kills you get maxes out at 10 kills for a total of 20 damage
Reaper.Ability2 = function(character,check)
local multiplier = PlayerKills[character.Name] or 0
if multiplier >= 10 then multiplier = 10 end
local Cooldown = 3
local AttackTime = 2.2
local Damage = 2 * multiplier
if check then
return Cooldown, AttackTime, Damage
else
print("Multiplier:",multiplier)
PlayerKills[character.Name] = 0 -- reset
end
end
CombatHandler.PlayerGotKill:Connect(function(player,amountOfKills)
if ClassHandler:GetPlayersClass(player) ~= "Reaper" then return end -- if their class isn't a reaper ignore them
PlayerKills[player.Name] = amountOfKills
end)
-- CombatHandler
if damage > humanoid.Health and humanoid.Health > 0 then
if not PlayerKills[player.Name] then
PlayerKills[player.Name] = 1
else
PlayerKills[player.Name] = PlayerKills[player.Name] + 1
end
CombatHandler.PlayerGotKill:Fire(player,PlayerKills[player.Name])
end
for the first thing, the only improvement I see is using math.clamp rather than logic to check if the number is over a cap
local PlayerKills = {} -- seperate from the CombatHandler and used for the reapers passive ability
-- damage increases the more kills you get maxes out at 10 kills for a total of 20 damage
Reaper.Ability2 = function(character,check)
local multiplier = math.clamp(PlayerKills[character.Name] or 0, 0, 10) --math.clamp can be used to easily set upper and lower bounds for numbers without needing logic
local Cooldown = 3
local AttackTime = 2.2
local Damage = 2 * multiplier
if check then
return Cooldown, AttackTime, Damage
else
print("Multiplier:",multiplier)
PlayerKills[character.Name] = 0 -- reset
end
end
CombatHandler.PlayerGotKill:Connect(function(player,amountOfKills)
if ClassHandler:GetPlayersClass(player) ~= "Reaper" then return end -- if their class isn't a reaper ignore them
PlayerKills[player.Name] = amountOfKills
end)
If you really want to squeeze every last drop of performance out of this, you could just use logic to do all of the top stuff in 1 line
-- CombatHandler
if damage > humanoid.Health and humanoid.Health > 0 then
local PlayerSpecificKills = PlayerKills[player.Name]
PlayerKills[player.Name] = PlayerSpecificKills and PlayerSpecificKills + 1 or 1 --if PlayerKills[PlayerName] then PlayerKills[PlayerName] + 1 else 1
CombatHandler.PlayerGotKill:Fire(player,PlayerKills[player.Name])
end