-
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
-
I have tried this on my own by simply adding
XP.Value -= 25where 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. -
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 ![]()
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)