Saving leaderstats will not work

Hi I am the YouTuber Kyky 200! (this is my alt)

I am making an anti-hack for a YouTube showcase.

I have been using a leaderstats loader forever, but now they wont work. It is super useful for my anti hack, And I need it.

I looked on DevFourm for solutions, but none have come.

This is the script. The saving part is not working, specifically

currency1.Value = ds1:GetAsync(player.UserId) or 0

Anything will help. Thanks in advance!

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("KicksValueSaver")

players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder") or player.leaderstats
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency1 = Instance.new("IntValue")
	currency1.Name = "Kicks"
	currency1.Parent = player.leaderstats
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
end)

You don’t need the “or player.leaderstats” in your 6th line, it can remain as

local folder = Instance.new("Folder")

edit: I forgot to give you an api reference sheet that you can use to compare your code, and learn off of.
https://developer.roblox.com/en-us/articles/Data-store

As far as I can tell, this code works properly. The problem could potentially be that your updating leaderstat values from a LocalScript. If this is the issue, convert the code into a script, put the script into ServerScriptService and place your code in a game.Players.PlayerAdded:Connect(function())

EDIT: Depending on the method of updating, it may be best to combine the script I suggested you make and the leaderstats into one script.

Both are the soulution. 30chars

1 Like

Wait nvm it still will not work

I need line 13 to be fixed. It is in server script alr

Make sure you mark a solution, as this thread might get bumped because there was no solution.

I do not have a solution yet. 30charsss

Can I have a refreshing of the script?

I updated very little, but here it is.

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("KicksValueSaver")

game.Players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	
	local currency1 = Instance.new("IntValue")
	currency1.Name = "Kicks"
	currency1.Parent = folder
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
end)

The error is on line 13.

What is the error you are getting?

19:09:46.058 - 502: API Services rejected request with error. HTTP 403 (Forbidden)

Are studio api services enabled?

No 30charsssssssssssssssssssss

1 Like

That’s the issue. Go to www.roblox.com < Inventory < Places < Choose a place < Three dots on the top right


< Configure This Game < Basic Settings < Enable Studio API Services

There are a few issues with your script.

  1. Never save data with the .Changed event, as it can throttle if the value changes too often.
    Only save every X seconds, when the player leaves and set up a BindToClose function.

  2. Wrap your calls in a pcall():
    This is used for error handling.
    It will execute the function, and if the function errors, it will handle it and give you the error, rather than erroring in your main thread and stopping.

  3. What is the purpose of this?

local folder = Instance.new("Folder") or player.leaderstats

It should look like this:

local folder = Instance.new("Folder")

And, NEVER use the parent argument that comes with Instance.new(), as this is slower.
Assign the properties first, then parent it.

1 Like