Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i save my leaderstats?

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

✓ Accepted answer

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)

8 comments

1

thank you

0

Make sure to mark the answer as correct if it helped :)

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?

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

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'

0

the HTTP Api is turned on, and i still get the same error

0

Did you try it in the game itself?

0

yes

This discussion is preserved as it was. New answers, comments, and votes are closed.