How to subtract XP stats on leaderboard every time player is killed/dies?

  1. I need XPs to subtract from the leaderboard when they died. Basically, when a player dies, their Deaths stats should -1 and XPs stats should -25

  2. I have tried this on my own by simply adding XP.Value -= 25 where I think it should go and it works perfectly; however it breaks the Kills system in the Kills/Deaths leaderboard, meaning that the Kills stats don’t add up when a player kills another player.

  3. I have tried looking for other solutions in the DevForum but I’m afraid that following those solutions could break what I have since my kill/death/xp leaderstats is a bit more complex as it includes xp saving into a datastore

Here’s the script. Any help is appreciated, thanks in advanced :+1:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Kills = Instance.new("IntValue", leaderstats)
	Kills.Name = "Kills"
	local Deaths = Instance.new("IntValue", leaderstats)
	Deaths.Name = "Deaths"
	local XP = Instance.new("IntValue", leaderstats)
	XP.Name = "XP"

	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-XP")
	end)

	if success then
		XP.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
	player.CharacterAdded:Connect(function(Character)
		Deaths.Value += 1
		XP.Value -= 25 --(SUPPORT HERE) I tried putting this line in but then players don't get a kill point when they kill another player
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							Kills.Value += 1 
						end
					end
				end
			end)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-XP", player.leaderstats.XP.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		warn("There was an error when saving data") 
		warn(errormessage)
	end
end)

game:BindToClose(function()
	task.wait(5) 
end)

Lua doesn’t support += or -=, so instead you should just use Kills.Value = Kills.Value + 1 and XP.Value = XP.Value - 25

Also after looking over your script I think you’re adding the Kill to the player who died, not the player who killed

1 Like

Lua doesn’t support += or -= , so instead you should just use Kills.Value = Kills.Value + 1 and XP.Value = XP.Value - 25

I think that doesn’t really matter because everything (kills/deaths/xp) worked fine before I added XP.Value -= 25 myself, the thing is that once I placed XP.Value -= 25 in the wrong place things started causing problems afterwards so I just need to know where to put XP.Value -= 25 in the right place

Right after Humanoid.Died:Connect(function() should work

Lua does support += and -=

(30char)

1 Like

Turns out it does as of 2020. Oops

1 Like