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
2 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

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

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 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

local dss = game:GetService('DataStoreService')
local 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.

game.Players.PlayerAdded:connect(function(player)
    local fold = Instance.new("Folder", player)
    fold.Name = "leaderstats"
    local stat = Instance.new("IntValue", fold)
    stat.Name = "Rings"
    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
end)

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.

game.Players.PlayerRemoving:Connect(function(player)
    local success, err = pcall(function()
        store:SetAsync(player.UserId,player.leaderstats.Rings.Value)
    end)
    if success then
        print('Yay')
    else
        print(err)
    end
end)

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:

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

game.Players.PlayerAdded:connect(function(player)
    local fold = Instance.new("Folder", player)
    fold.Name = "leaderstats"
    local stat = Instance.new("IntValue", fold)
    stat.Name = "Rings"
    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
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, err = pcall(function()
        store:SetAsync(player.UserId,player.leaderstats.Rings.Value)
    end)
    if success then
        print('Yay')
    else
        print(err)
    end
end)
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 — 2y
0
You could add maybe like, game:BindToClose() as well. Punctist 120 — 2y
Ad
Log in to vote
1
Answered by
mkn00b 5
2 years ago
Edited 2 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

local data = game:GetService("DataStoreService") --get the datastore
lvl = data:GetDataStore("Rings") -- add a store for your rings

game.Players.PlayerAdded:Connect(function(plr) --when a player joins
    local Lstats = Instance.new("Folder") --creating a folder for the leaderstats
    Lstats.Name = "leaderstats" --give it a name

    local rgs = Instance.new("IntValue") --add the value for rings
    rgs.Parent = Lstats -- put the variable in the leaderstats
    rgs.Name = "Rings"  --calls the value rings

    Lstats.Parent = plr --put the leaderstats folder in the right position
    wait()
    plr.leaderstats.Rings.Value = lvl:GetAsync(plr.UserId) or 0 --set the variable if he has data or ales it's 0
end)


game.Players.PlayerRemoving:Connect(function(plr) --when a plyer leaves
    lvl:SetAsync(plr.UserId, plr.leaderstats.Rings.Value) --save the data
end)

Answer this question