local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetOrderedDataStore("PlayerData")
local teams = game:GetService("Teams")
local free = teams.Free
local leaderboard = workspace:FindFirstChild("Leaderboard")
local updateEvent = leaderboard:FindFirstChild("UpdateEvent")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local arrests = Instance.new("IntValue")
arrests.Value = 0
arrests.Name = "Arrests"
arrests.Parent = leaderstats
local arrested = Instance.new("IntValue")
arrested.Value = 0
arrested.Name = "Arrested"
arrested.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data = playerData:GetAsync(playerUserId)
if data then
arrests.Value = data["Arrests"]
arrested.Value = data["Arrested"]
-- plr.Team = data["Team"]
-- print(plr.Team)
else
arrests.Value = 0
arrested.Value = 0
-- plr.Team = free
-- print("put on free team")
end
end)
local function createTable(player)
local playerStats = {}
for i,stat in pairs(player.leaderstats:GetChildren()) do
playerStats[stat.Name] = stat.Value
end
--playerStats["Team"] = player.Team.Name
return playerStats
end
local function onPlayerExit(player)
local playerStats = createTable(player)
print(playerStats)
local success, errorMessage = pcall(function()
local playerUserId = "Player_"..player.UserId
print(playerUserId)
playerData:SetAsync(playerUserId,playerStats)
end)
if not success then
warn("error when saving data")
end
end
game.Players.PlayerRemoving:Connect(onPlayerExit)
--game:BindToClose(onPlayerExit)
while true do
for i,v in ipairs(game.Players:GetChildren()) do
local leaderstats = v:FindFirstChild("leaderstats")
if leaderstats then
local arrests = leaderstats:FindFirstChild("Arrests")
if arrests then
local success, err = pcall(function()
playerData:SetAsync(v.Name,arrests.Value)
end)
end
end
end
updateEvent:Fire()
wait(60)
end
In the game, the player's arrests and arrested values are saved, and then put on a leaderboard for the top 5 player's in the game (the while true do loop). However the player's data is just not saving. I was thinking maybe it had something to do with playerData:SetAsync(v.Name,arrests.Value) and playerData:SetAsync(playerUserId,playerStats)
being a bit different in the way data is saved but i could be wrong.

1 comment
b