My datastore wont work

I made a datastore script, but it won’t work; I’m searching for help.

Output:

image

Script:

local ds = game:GetService("DataStoreService"):GetDataStore("--val01")

game.Players.PlayerAdded:Connect(function(player)
	local key = "vls-"..player.userId
	local folder = Instance.new("Folder")
	folder.Parent = player
	folder.Name = "leaderstats"
	local currency = Instance.new("NumberValue",folder)
	currency.Name = "Warns"
	currency.Value = 0
	
	local save = ds:GetAsync(key)
	if save then
		currency.Value = save
	end
	currency.Changed:Connect(function()
		ds:SetAsync(key,currency.Value)
	end)
end)

I searched a solution, but the error continued.

P.D: I have api services on and I published the game

1 Like

Did you publish your game? …

2 Likes

You should also save data when the player leaves the server.

1 Like

I did, but it didn’t work anyways.

1 Like

Thanks!, I’ll try so I can see if it works :stuck_out_tongue:

1 Like

You also have to allow http in the game setting

1 Like

Have you tried going the Game Settings on the Roblox website? Sometimes it doesn’t save the API setting from inside Studio.

2 Likes

local save = ds:GetAsync(key)
using pcall is recommended

3 Likes

I did turn it on, but same output.

1 Like

Did you use pcall for getasync?
Because new players won’t have their datastore loaded

1 Like

image
I did too : P

1 Like

like this

local Save
local suscess, errormsg = pcall(function()
Save = ds:GetAsync(key)
end)

if sucsess and Save then
currency.Value = save
end

2 Likes

The error is suggesting that Studio access is still disabled. Perhaps it could be as simple as closing Studio for a few minutes and reopening it.

2 Likes

I entered the game on roblox (not studio), and it seems to work now :neutral_face:.
anyways I don’t know why it did not work on studio, I’ll search on devforum because I’m curious :sweat_smile:
Thank you all for your replies and help :3 .

Under Game Settings in studio, you have to enable Studio access to APIs. If you don’t have that enabled, getting and saving data in studio will fail.

Contrary to @jihooandjeaniscool’s response, HTTP does not need to be enabled in order to use Roblox’s built-in datastores.

4 Likes

There are a couple reasons why you data store doesn’t work. 1 make sure the game is published and the Enable Studio Access for API Services is on and don’t save you data each time the value changes. That can mess your game. Either save it each time a player leave and auto save with 1 minute interval

3 Likes