Datastore Not Saving

Hey Developers,

I made this Datastore for my points system and it’s been working lately until now when I got an error in the console, can anyone help?

Script:

local DS = game:GetService("DataStoreService")
local PointsStore = DS:GetDataStore("Points")

local success, Error = pcall(function()
		data = PointsStore:GetAsync(player.UserId.."Points")
	end)
	if success then
		points.Value = data
	else
		warn(Error)
	end

Error:

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

1 Like

Are you testing in studio?

If so then you probably don’t have Studio Access to API Services enabled.

1 Like

First, do what @sjr04 said, then change it to this:

local DS = game:GetService("DataStoreService")
local PointsStore = DS:GetDataStore("Points")

local data

local success, Error = pcall(function()
	data = PointsStore:GetAsync(player.UserId.."-Points")
end)
	
if success then
	points.Value = data
else
	warn(Error)
end
1 Like

Ok, I turned that on and I am getting this error now:
20:00:17.080 - ServerScriptService.Leaderboard:31: invalid argument #3 (string expected, got nil

When you click on it it takes you to this:

points.Value = data

I am still getting this:
20:00:17.080 - ServerScriptService.Leaderboard:31: invalid argument #3 (string expected, got nil

When you click on it it takes you to this:

points.Value = data

That means points is a StringValue. And nil was assigned to its Value property. So you probably don’t have any data saved under your user ID.

So you can give a default value like "Default" or anything.

1 Like

This is what I have to assign a default value:

local points = Instance.new("StringValue", leaderstats)
	points.Name = "Points"
	if points ~= 0 then
		points.Value = 0
	end

Just make “points” an IntValue

1 Like

Your points variable always points to a StringValue instance. But you are comparing the reference against 0. points ~= 0 always evaluates to true. You meant points.Value ~= 0

1 Like

I got this:

invalid argument #3 (string expected, got nil)

It seems Its fixed when I made it a IntValue. But I tried to change my value via explorer then I stopped the test then joined again and it didn’t save.

Is Studio access to API Services enabled?

1 Like

Yes, it is enabled. 30chars30chars

Try using BindToClose function If not, test ingame

1 Like

This is my script right now:

local DS = game:GetService("DataStoreService")
local data
local PointsStore = DS:GetDataStore("Points")

local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	if points ~= 0 then
		points.Value = 0
	end
	playerStats[player] = leaderstats

	local success, Error = pcall(function()
		data = PointsStore:GetAsync(player.UserId.."-Points")
	end)
	
	if success then
		points.Value = data
	else
		warn(Error)
	end

You don’t have player added or player removing to save the data? Or is it just not included?

1 Like

It is just not included, my whole script is this:

local DS = game:GetService("DataStoreService")
local data
local PointsStore = DS:GetDataStore("Points")
local playerStats = {}
group = script.GroupID.Value
default = script.DefaultRank.Value

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	local grouprank = Instance.new("StringValue", leaderstats)
	grouprank.Name = "Rank"
	local rank = player:GetRoleInGroup(group)
	if rank ~= "Guest" then
		grouprank.Value = rank
	else
		grouprank.Value = default
	end

	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	if points ~= 0 then
		points.Value = 0
	end
	playerStats[player] = leaderstats

	local success, Error = pcall(function()
		data = PointsStore:GetAsync(player.UserId.."-Points")
	end)
	
	if success then
		points.Value = data
	else
		warn(Error)
	end
end)
3 Likes

You’re not sanitizing the response from GetAsync. Make sure you check if there even is a data before you set the IntValue’s value to nil.

What’s happening is:
-Getting datastore
-Getting your points
-It returns that there’s no data there so it’s nil
-You try setting points.Value to nil

It’s a one line if statement that you can attach to


To fix the not saving part - can you please post the code that actually saves the value?

So far you only load data. There is no reference to saving data that I can see in your script.

1 Like

Are you running this code on a group game that you have build access to? If so, try publishing it onto your profile, keeping it private and testing it there to see if your error goes away. I ran into this issue a while back and for some reason even though I had edit access for the game, I couldn’t access data stores.

pcalling the code is a good idea in general, but it won’t fix your underlying issue, it will only mask the side-effects, creating a nasty data loss bug if not handled properly.

1 Like

Yes, Its under a group game but I own the group.