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

how can i make a saving leaderstat?

Asked by
omorizz 50
3 years ago

i have this script in serverscriptservice and i want to make "rings" saveable when you leave and rejoin the game but every tutorial ive watched isnt working for me

1game.Players.PlayerAdded:connect(function(player)
2    local fold = Instance.new("Folder", player)
3    fold.Name = "leaderstats"
4    local stat = Instance.new("IntValue", fold)
5    stat.Name = "Rings"
6end)

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Be sure to enable access to API service in game settings..

You can use the datastoreservice https://developer.roblox.com/en-us/articles/Data-store There are two basic methods. :GetAsync which grab data from a given key :SetAsync which stores data with a given key and given value

Let me explain what it does These two methods work on a datastore we create.

By doing that, we do

1local dss = game:GetService('DataStoreService')
2local store= DataStoreService:GetDataStore("NameYourDataStore")

We got the stats 'Rings' And we got the event player added which fires when a player joins. We can use GetAsync to get the data for the player's stat. If the player is new, the getasync that fires won't return data since setasync didn't fire when they left. But wait, I didn't put setasync or getasync to an example.

What is a key? A key is a way to access your data. Setasync has two args. (A Key, A value to store) While getasync only has one args which is the key because it gets just the value by the key and returns the value associated with the key.

1game.Players.PlayerAdded:connect(function(player)
2    local fold = Instance.new("Folder", player)
3    fold.Name = "leaderstats"
4    local stat = Instance.new("IntValue", fold)
5    stat.Name = "Rings"
6    stat.Value = store:GetAsync(player.UserId) or 0 -- What we can do is get the data by the key but if there is no data incase the player is new, it does or 0
7end)

Now it's time for setasync! We can use it when a player leaves to save the player's stat. We can do any key such as the player name or player id but it's recommended for the player id because the player can change the username.

You can also do (player.UserId..'Rings') but then we need to change getasync to player.UserId..'Rings' but it's up to you for the key but make sure you get the key properly when getting data.

01game.Players.PlayerRemoving:Connect(function(player)
02    local success, err = pcall(function()
03        store:SetAsync(player.UserId,player.leaderstats.Rings.Value)
04    end)
05    if success then
06        print('Yay')
07    else
08        print(err)
09    end
10end)

Now we saved the player's data by using the userid as the key and the rings value. But again, you can also do player.UserId..'Rings' or anything.

But why is there a pcall function? It is to check if the data save successfully. If not, we can check if there is an error by printing it.

Now you're done!

Full Script:

01local dss = game:GetService('DataStoreService')
02local store= DataStoreService:GetDataStore("NameYourDataStore")
03 
04game.Players.PlayerAdded:connect(function(player)
05    local fold = Instance.new("Folder", player)
06    fold.Name = "leaderstats"
07    local stat = Instance.new("IntValue", fold)
08    stat.Name = "Rings"
09    stat.Value = store:GetAsync(player.UserId) or 0 -- What we can do is get the data but if there is no data incase the player is new, it does 0
10end)
11 
12game.Players.PlayerRemoving:Connect(function(player)
13    local success, err = pcall(function()
14        store:SetAsync(player.UserId,player.leaderstats.Rings.Value)
15    end)
View all 21 lines...
0
Quick note: it may not work in studio because once you leave, the script doesn't save since the server automatically closes. AProgrammR 398 — 3y
0
You could add maybe like, game:BindToClose() as well. Punctist 120 — 3y
Ad
Log in to vote
1
Answered by
mkn00b 5
3 years ago
Edited 3 years ago

i just made a script for this, i will give it you: i changed some stuff so if it doesnt work i will fix it

01local data = game:GetService("DataStoreService") --get the datastore
02lvl = data:GetDataStore("Rings") -- add a store for your rings
03 
04game.Players.PlayerAdded:Connect(function(plr) --when a player joins
05    local Lstats = Instance.new("Folder") --creating a folder for the leaderstats
06    Lstats.Name = "leaderstats" --give it a name
07 
08    local rgs = Instance.new("IntValue") --add the value for rings
09    rgs.Parent = Lstats -- put the variable in the leaderstats
10    rgs.Name = "Rings"  --calls the value rings
11 
12    Lstats.Parent = plr --put the leaderstats folder in the right position
13    wait()
14    plr.leaderstats.Rings.Value = lvl:GetAsync(plr.UserId) or 0 --set the variable if he has data or ales it's 0
15end)
16 
17 
18game.Players.PlayerRemoving:Connect(function(plr) --when a plyer leaves
19    lvl:SetAsync(plr.UserId, plr.leaderstats.Rings.Value) --save the data
20end)

Answer this question