I wanted to use the same leaderboard saving script that was in my other game working and its not working. (Windows 10 OS [Operating System] - Roblox)
Since its gui-game, with not visible leaderboard and players won’t see it, i needed this for something other
Script:
Normal Script, Workspace
local DataStoreService = game:GetService("DataStoreService")
local pdata = DataStoreService:GetDataStore("playerDatamogus23")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local value = Instance.new("IntValue")
value.Name = "Times"
value.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = pdata:GetAsync(player.UserId)
end)
if success then
value.Value = data
else
print("there was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
pdata:SetAsync(player.UserId, player.leaderstats.Times.Value)
end)
if success then
print("your data got saved wheres my thanks")
else
print("there was an error")
warn(errormessage)
end
end)
When using BindToClose,
you’ll need to save their data there too.
[Loop through all players is a possible option]
As you did in the PlayerRemoving :
for _,Player in pairs(game.Players:GetPlayers()) do
local success, errormessage = pcall(function()
pdata:SetAsync(Player.UserId, Player.leaderstats.Times.Value)
end)
if success then
print("your data got saved wheres my thanks")
else
print("there was an error")
warn(errormessage)
end
end
I probably found an error, with this script is everything okay, its something wrong with my testing system (just textbutton that adds value). When i am doing that in this script by adding while true do loop that sets value to old value + 1 everything is working. Thanks for your help I will try to make it work as I want.