How would i make something like a assist system?

Basically i wanna make something like when players kill a boss the rewards are split via damage each player did (percentage)

Something like
A boss with 1000$ rewards
Player1 Did 45% of the damage and received 450$
Player2 Did 37% of the damage and received 370$
And so on

If you have a damage system and you can track how much damage each player dealt, then you have to calculate the percentage by dividing all of the enemy dealt damage by enemy HP. After finding the ratio(it might look like 0.2 as in 20% of damage done by the player) what is left is to specify the max winning amount and calculate the percentage of winnings to be given to a certain player. Example: if player dealt 40% of damage or 0.4 and max winnings are 1000$ then 1000 * 0.4 is 400$ - final winnings. Other player who dealt 0.6 of damage (since 1 - 0.4 is 0.6) will receive 600$.

Hmm yeah that does look like it could work, although i mainly need help on how i would be able to track how much damage each player has done

If the boss has a humanoid you can track when it dies.

When a player does damage do:
bossHumanoid:SetAttribute(attacker.Name, numDamage)

You should make a conditional statement that checks if the boss humanoid already has damage from a certain player, and if it does, just take their existing damage and add more.

After the boss humanoid dies, you would check the values.

local humAttributes = bossHumanoid:GetAttributes()
for playerName, dmg in pairs(humAttributes) do
    print(playerName.." did "..dmg.." damage!")
    -- Here you would do your percentage calculations local reward = dmg / bossReward
end

Alright with this i set up

if Humanoid:GetAttribute(Player.name) then
	local Attribute = Humanoid:GetAttribute(Player.name)
	Humanoid:SetAttribute(Player.name, Attribute + 12)
else
	Humanoid:SetAttribute(Player.name, 12)
end

it does work, although one problem i have is that it sometimes goes over the health,
As in ill kill the enemy and the attribute says ive done 108 damage but the humanoid had only 100 hp
dont know what i can do to fix this

1 Like

Apologies for the late response,

local clampDmg = math.clamp(12, 0, 100) -- math.clamp(x, min, max)
Humanoid:SetAttribute(Player.name, clampDmg)

This should clamp the damage so that it can’t exceed 100. If your boss has more than 100 hp you can simply set 100 to bossHumanoid.MaxHealth

You should also add a conditional statement in your damage code.

local curHealth = bossHumanoid.Health -- health BEFORE you damage the boss
if rawDamage > curHealth   then -- if this is the final blow that kills the boss:
    local Attribute = Humanoid:GetAttribute(Player.name)
    local trueDamage = rawDamage - curHealth
    Humanoid:SetAttribute(Player.name, Attribute + trueDamage)
end
1 Like

Alright

Used this as

Humanoid:SetAttribute(Player.name, Attribute + math.clamp(12, 0, Humanoid.Health))

It works, :+1:
Just a quick question, should i still do this

if math.clamp already works?

Whenever a player damages a boss, log their damage into a counter, preferably in a dictionary or in anyway you use to keep track of damage, add their damage to a total. Then, when the boss dies, use this formula to get the percent of how many players did damage;

local PercentOfDamage = (playerDamage / BossTotalHp) * 100

After you have the percent, multiply that percent number by 100 for the reward

local reward = PercentOfDamage * 100

This is only the code i understood to make from your description, so you will need to modify it if needed

Alright i get it, but what should i do if it gets alot of decimals like 61.29032258064516 or something
Should i just use math.round?

Yes, you could use math.round() or you could just round the number to the nearest tenth

a little late but for

I would just mutliply the number by 10 then round it then divide it by 10? or is there a better way of doing this

yeah you can do that

local number = 123.456

local rounded = math.floor(number * 10)/10
print(rounded) -- 123.4