Majority Damage Reward

So i want to make it where the player that does the most damage gets the exp , My Temporary solution is i created a table that holds the players name along with the damage

the script for doing damage is:

local Damagers = {
Player.UserId = 123456, -- how ever much damage they do
Player.UserId = 654321
}

all i need help with is how to table.insert the Player.UserId along with the Number this was my first try but it didnt work

table.insert(Damagers,Player.UserId,X) -- X being the amount of damage i do
2 Likes

You have to do some scripting in the weapons to keep track of who dealt damage

no thats what im stuggling to find , i tryied doing a table , but idk how to keep 2 linked things in 1 value , or how to add values to values

Make a folder in the mob, when a player hits the mob, create an intvalue inside the folder if doesn’t exist, name the int value the player’s name/userid, and plus one to that int value, and compare who has the largest int value when the mob is killed and grant that player exp

Why would you need two linked things to one value? Just player’s ID and the damage they’ve done. But if you really need to link two things to one table you would create a dictionary like :

local damage = {
[player1.UserId] = {damage, whatever else you want to store},
}

Also without any context to how you damage the player’s it’s hard to help you, but wherever your server event is you should easily be able to fetch the player that damaged from the function being fired and then you can also hold your table in that script and just add the damage to the damage in the table.

ok this can work , but how would i table.insert the playerid along with the other values?

table.insert(player.UserId, damage.Value)

nope, thats not how to do it. This is how:

table.insert(damage[player.UserId], dealt)
1 Like

Im not in studio right now so I could just do this from the top of my head I may be wrong tho, you could use it as refrence.

Assuming you already have the values of the player damages, all you would have to do is just add the damage to the player.

PlayerDamage.Value = PlayerDamage.Value + Dmg

Then you could insert all the player values to one table at the end of the round.

table.insert(End_Round_Calc_Dmg, PlayerDamage.Value)

Then sort it out, the person with the most damage gets XP. (using 3 players)

local function SortDamage()
	local TopPlayerDamage;
	
	if End_Round_Calc_Dmg[1] > End_Round_Calc_Dmg[2] then
		TopPlayerDmg = End_Round_Calc_Dmg[1]
		if End_Round_Calc_Dmg[1] < End_Round_Calc_Dmg[3] then
			TopPlayerDmg = End_Round_Calc_Dmg[3]
		end
	else
		if End_Round_Calc_Dmg[2] < End_Round_Calc_Dmg[3] then
			TopPlayerDmg = End_Round_Calc_Dmg[3]
		end
	end
	
	return TopPlayerDamage;
end

Then lets not forget we have to check every player’s damage to compare to the most damage to see which player we should give the XP to:

if PlayerDamage.Value == SortDamage() then
	-- Give Player XP
end

Handle this through the server.
I’m not sure if this work, but you could give it a try man.

Inside a weapon server script:
(Starts at line 114 in the studio file under StarterPack>ClassicSword>SwordScript)

humanoid:TakeDamage(Damage)
	if humanoid:FindFirstChild("DamageTracking") ~= nil then
		if humanoid.DamageTracking:FindFirstChild(Tool.Parent.Name) == nil then
			local FolderP = Instance.new("Folder", humanoid.DamageTracking)
			if Tool.Parent.Name ~= "Backpack" then
				FolderP.Name = Tool.Parent.Name
			else
				FolderP.Name = Tool.Parent.Parent.Name
			end
			local DmgVal = Instance.new("IntValue", FolderP)
			DmgVal.Name = "TotalDamage"
			DmgVal.Value += Damage
		else
			humanoid.DamageTracking:FindFirstChild(Tool.Parent.Name).TotalDamage.Value += Damage
			if humanoid.Health <= Damage and humanoid.Health > 0 then
				local Highest = -math.huge
				local HighestDamagePlayer = nil
				for i,v in pairs(humanoid.DamageTracking:GetChildren()) do
					if v.TotalDamage.Value > Highest then
						Highest = v.TotalDamage.Value
						HighestDamagePlayer = v.Name
					end
				end
				if game.Players:FindFirstChild(HighestDamagePlayer) ~= nil then
					game.Players:FindFirstChild(HighestDamagePlayer):WaitForChild("leaderstats"):WaitForChild("XP").Value += 5
				end
			end
		end
	else
		local Folder = Instance.new("Folder", humanoid)
		Folder.Name = "DamageTracking"
		if humanoid.DamageTracking:FindFirstChild(Hit.Parent.Name) == nil then
			local FolderP = Instance.new("Folder", Folder)
			if Tool.Parent.Name ~= "Backpack" then
				FolderP.Name = Tool.Parent.Name
			else
				FolderP.Name = Tool.Parent.Parent.Name
			end
			local DmgVal = Instance.new("IntValue", FolderP)
			DmgVal.Name = "TotalDamage"
			DmgVal.Value += Damage
		end
	end

Leaderboard script:
ServerScriptService>Leaderboard

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder", player)
	Folder.Name = "leaderstats"
	local Xp = Instance.new("IntValue", Folder)
	Xp.Name = "XP"
end)

Studio File:
XP.rbxl (53.2 KB)

This is just a quick attempt for you to see the concept, don’t expect it to be perfect

so i wanted to do a table like this

local Damagers = {
player.id = {Dmg = 13} -- how ever much damage
}

so that way i could just do

player.id.Dmg + x 

how would i insert a players.Id and make the damage

One possible solution would be to use a dictionary to keep track of what players did damage to the entity (and how much) like so:

local entityDamage = {
    ["playername"] = 100
}

local highest
for i, v in pairs(entityDamage) do
   if not highest or v > entityDamage[highest] then
       highest = i
   elseif highest and v == entityDamage[highest] then
       -- idk how you want to handle it if it's shared
   end
end

yea this is how i was planning on doing it , i just dont know how to table.insert the players.id along ith the value

That’s fairly easy, you just need to do

t[player.UserId] = t[player.UserId] and t[player.UserId] + damage or damage

(damage being the amount of damage being done to the entity)

how do i add a player to the table , because when i do

table.insert(entityDamage,player.Name)

then do

for i, v in pairs(entityDamage) do
   if not highest or v > entityDamage[highest] then
	highest = i
	print(highest)
elseif highest and v == entityDamage[highest] then
   print(highest)
end
end

the error message " 01:07:19.794 - Workspace.KreatorKols.LocalScript:12: attempt to compare string and number" comes up

The solution i found is

entityDamage[script.Parent.Name] = dmg

how would you find the 2nd or 3rd highest?