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