I've been trying to make a deaths/kills leaderboard and save the data, but everytime I add anything about DataStoreService or just data saving, the leaderboard only shows the names of players but not kills or deaths. I've tried it in game and in studio and still doesn't work.
My code is compiled from different articles because I'm still quite new to Lua(I can code well in other languages). Anyways, here is my code, any help will be appreciated:
local DataStoreService = game.GetService("DataStoreService")
local killsDataStore = DataStoreService:GetDataStore("KOstats")
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Parent = leaderstats
local kills = Instance.new("IntValue")
kills.Name = "KOs"
kills.Parent = leaderstats
local data
local success,errormessage = pcall(function() --Message for success or error
data = killsDataStore:GetAsync(player.UserId.."deaths")
end)
if success then --Checks if data is successfully loaded
kills.Value = data
else
print("There was an error while getting data")
warn(errormessage) --Warn is like print, but in yellow
end
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
deaths.Value = deaths.Value + 1
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- When player is removed do
local success,errormessage = pcall(function()
killsDataStore:SetAsync(player.UserId.."deaths",player.leaderstats.kills.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error when saving data!")
warn(errormessage)
end
end)
