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

How do i save my leaderstats?

Asked by 4 years ago

Hello there im very clueless and i dunno what to put here so yea

game.Players.PlayerAdded:Connect(function(plr)

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = plr

local Power = Instance.new("IntValue",leaderstats)

Power.Name = "Power"

Power.Value = 0

local Money = Instance.new("IntValue",leaderstats)

Money.Name = "Energy"

Money.Value = 0

end)

how do i save this?

1 answer

Log in to vote
0
Answered by
Torren_Mr 334 Moderation Voter
4 years ago

You need to use the DataStore Service

Here is the script.

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PowerValueSaver") --Runs power server
local ds2 = datastore:GetDataStore("MoneyValueSaver") --Runs money server

players.PlayerAdded:connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local Power = Instance.new("IntValue")
    Power.Name = "Power"
    Power.Parent = plr.leaderstats
    Power.Value = ds1:GetAsync(plr.UserId) or 0 --Checks if power is saved or sets to 0
    ds1:SetAsync(plr.UserId, Power.Value) --Saves power

    local Money = Instance.new("IntValue")
    Money.Name = "Energy"
    Money.Parent = plr.leaderstats
    Money.Value = ds2:GetAsync(plr.UserId) or 0 --Checks if Money is saved or sets to 0
    ds2:SetAsync(plr.UserId, Money.Value) -- Saves Money

    Power.Changed:connect(function()
        ds1:SetAsync(plr.UserId, Power.Value) --Saves when Power is changed
    end)
    Money.Changed:connect(function()
        ds2:SetAsync(plr.UserId, Money.Value) --Saves when Money is changed
    end)

end)
1
thank you Arcsika2008 8 — 4y
0
Make sure to mark the answer as correct if it helped :) Torren_Mr 334 — 4y
0
so umm im in team create and i get the error: API Services rejected request with error. HTTP 403(Forbidden) what can i do? Arcsika2008 8 — 4y
0
Oh, make sure your game has HTTP enabled. Read about how to enable it here: https://developer.roblox.com/en-us/api-reference/class/HttpService Also enable API Torren_Mr 334 — 4y
View all comments (4 more)
0
The API error is completely normal, due to an API not being enabled in studio. It should work completely fine in-game, although if you want it to work in studio you can find the option to turn it on in 'Configure this Game' pwx 1581 — 4y
0
the HTTP Api is turned on, and i still get the same error Arcsika2008 8 — 4y
0
Did you try it in the game itself? Torren_Mr 334 — 4y
0
yes Arcsika2008 8 — 4y
Ad

Answer this question