Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Leaderboard stats aren't saving?

Asked by 7 years ago
01scoreKey = "PlayerScore"
02game.Players.PlayerAdded:connect(function(player)
03local stat = Instance.new("IntValue")
04stat.Name = "leaderstats"
05stat.Parent = player
06local WinTokens = Instance.new("IntValue")
07WinTokens.Name = "WinTokens"
08WinTokens.Parent = stat
09WinTokens.Value = player:LoadNumber(scoreKey)
10end)
11 
12game.Players.PlayerRemoving:connect(function(player)
13    if player:FindFirstChild("leaderstats") then
14        player:SaveNumber(scoreKey, player.leaderstats.WinTokens.Value)   
15    end
16end)

This script is a normal script in serverscriptservice.

I read the wiki on data persistence and made this script, but it doesn't work. Whats the problem?

2 answers

Log in to vote
0
Answered by 7 years ago
01PlayerDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerScore")
02 
03game.Players.PlayerAdded:Connect(function(player)
04    local stats = Instance.new("Folder")
05    stats.Name = "leaderstats"
06    stats.Parent = player
07    WinTokens = Instance.new("IntValue")
08    WinTokens.Name = "WinTokens"
09    WinTokens.Parent = stats
10    WinTokens.Value = PlayerDataStore:GetAsync(player.UserId)
11 
12    player.leaderstats.WinTokens.Changed:Connect(function()
13        PlayerDataStore:SetAsync(player.UserId, WinTokens.Value)
14    end)
15end)
View all 21 lines...

If you've looked at the wiki and are having troubles putting it together, here's what it should be. When you use GetAsync(player.UserId) it returns a value attached with the player's id. That value is changed when you use SetAsync(player.UserId, WinTokens.Value) where WinTokens.Value is the value attached. I put an extra changed event in there so it always saves one the value changes. Hope this helps!

Ad
Log in to vote
0
Answered by 7 years ago

Data Persistence is old and deprecated, It's very buggy, try to learn the new way, using Data Stores.

http://wiki.roblox.com/index.php?title=Data_store

they're more reliable than data persistence

Answer this question