Saving Data Script Wont Work

Im new to Roblox Studios scripting so to learn some basic fundamentals I decided to make a simulator like game as practice. I wanted to make a script which saved the players progress as he/she progressed. This is what I scripted down, but when I tried testing out the code the players progress wouldn’t save. I checked the output monitor and didn’t get any error. Any idea what I did wrong and how to fix it? The script is a regular script inside of ServerScriptService.

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Sugar
	local save2 = plr.leaderstats.Cash
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else 
		local NumberForSaving = {save1.Value, save2.Value}
		DS:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Sugar.Value, plr.leaderstats.Cash.Value})
end)
2 Likes

Do you have another script or something that creates the actual folder leaderstats and intvalue for cash and sugar?

1 Like

I do.

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local sugar = Instance.new("IntValue")
	sugar.Name = "Sugar"
	sugar.Value = 0
	sugar.Parent = stats
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = stats
end)

Try using pcall() it tell show if theres error or not…

for exp:

local succ, err = pcall(function()
    -- Put the :GetAsync function here
end)

if succ then
 -- code
elseif err then
 warn(err) -- Shows if theres a error.
end

And try putting all of them in one script instead of making 2 scripts :slight_smile:

Some datastore tutorial:

Try saving a value before you leave the game and see if that solves your issue. It may be that your script is fully functional, but is just not reading the values in time before you leave the game.

How would I do this? (Im new to scripting)

You could make it so when they first join the game you use SetAsync once, then after you leave remove the code in studio, publish, and rejoin.

I was having troubles logging into studio so when I did this appeared Failed to load plugin permissions from cloud: loadPluginPermissionsPageAsync(0, 10): HTTP error: HttpRequest “PluginLoader::loadAllPluginPermissionsAsync()”: Got network error status: 502. I haven’t done what you said to do yet though. Does thsi have anything to do with it?

Well there’s a possibility that you don’t have your setting enabled for HTTP requests. So I advise you open up game settings in studio, and enable http requests. You should know whichever one you need to enable because it will note that enabling it allows datastores to be used.

Turns out the script was fine, Roblox Studios is just glitched when it comes to Data Storing. I tried it out on Roblox and it worked.