I have tried to save leaderstats, Kills and deaths. I have tried everything to no avail, Been stuck on this for 3 days now. Thanks for any support.
2 Likes
You’ll need to use a DataStore, although you likely already know this.
1 Like
DataStore is required for these kinds of things, you can learn Datastore by watching Videos of Tutorials from Youtube
1 Like
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local function deserializeData(player, data)
local leaderstats = player.leaderstats
for statName, statValue in next, data do
local stat = leaderstats:FindFirstChild(statName)
if statName then
stat.Value = statValue
end
end
end
local function serializeData(player)
local data = {}
local leaderstats = player.leaderstats
for _, stat in ipairs(leaderstats:GetChildren()) do
data[stat.Name] = stat.Value
end
return data
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local knockouts = Instance.new("IntValue")
knockouts.Name = "Knockouts"
knockouts.Parent = leaderstats
local wipeouts = Instance.new("IntValue")
wipeouts.Name = "Wipeouts"
wipeouts.Parent = leaderstats
local success, result = pcall(function()
return datastore:GetAsync("Stats_"..player.UserId)
end)
if success then
if result then
deserializeData(player, result)
end
else
warn(result)
end
end
local function onPlayerRemoving(player)
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
local function onServerShutdown()
for _, player in ipairs(players:GetPlayers()) do
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)
Here you go, I just wrote this multi-purpose script which is able to store all leaderstats (regardless of the types of values/names of values used).
13 Likes