I don’t know if there’s a better way but according to me, i would create a intvalue instance manually added from the damage code, make a folder specifically named something then set the value instance’s parent in the folder.
I would only add that code when some sort of weapon damages a non-familiar NPC, it would run that code also adding the applied damage dealt.
Don’t forget to also rename the value’s name to the damage dealer when the value and the folder is added in by the way.
Now, if it’s the same NPC that took damage already, it adds the damage again, now i don’t know how you would do this but i’ll start with the NPC’s death.
Now, make sure the folder is located in the NPC that took damage which was created from the damage script, when the NPC dies, it would make an if statement if there is a exactly named user with that int value’s name. If it finds it using the “for i v” strat, you can use another one if it suits you more.
Anyways, if the player has a leader statistic, you can manage it so the amount of value the int value had, it would add to that leader statistic’s specific value. You should also divide it by the NPC’s health so the player doesn’t gain a lot of money extremely fast, don’t also forget to delete the folder within the value to avoid some strange bugs i didn’t know.
You should also mess with the math to see if it gets an accurate value to what you want it to get.
If I understand you correctly, this must be the solution:
local NPCHealth = 200
local Reward = 1000
local Players = {} -- Table with players who did damage
-- After players will defeat a NPC, table will looks like this: {{"Player1", 120},...} (First is player's name, second is player's damage he did)
local function Reward()
for _, PlayerTable in pairs(Players) do
local Player = game.Players[PlayerTable[1]] -- Getting player
local PlayerDamage = PlayerTable[2]/NPCHealth -- Matching in % how much player did damage (For Player1 it will be 120/200 = 0,6 (60%))
local PlayerReward = Reward * PlayerDamage -- Matching how much gold player will get (for Player1 it will be 1000 * 0,6 = 600 Gold)
Player.leaderstats.Gold.Value += PlayerReward -- Adding PlayerReward to his stats
end
end
-- Remember that you somehow need to insert players' tables to Players