SaveData does not work in Roblox studio

Hello.

Problem:

Saving data does not work in Roblox studio (it used to work).

What do I mean by that?

for example, if I collect a certain number of coins in roblox Studio, then when I stop the game and start over, they will not be saved.

But if I collect them in the roblox game itself, they will be saved when I exit the game both in the game itself and in roblox studio

Why didn’t I attach the script?

Because at least it used to work without problems and I’m sure nothing has changed in it, especially since I even rewrote it, but nothing has changed at all

2 Likes

It would still be helpful to show the script because there might’ve been something you missed

This is normal in studio, this happens to most people and you shouldnt be worried

It happens because in studio the local server might close before the playerremoving event is fired

you should use game:BindToClose() to make sure data gets saved if the server somehow closes

Thanks for your answer and “game: Bind To Close ()” I already use in my script.

Well, if it can help:

local DSS = game:GetService(“DataStoreService”)

local dVersion = 1
local save = DSS:GetDataStore(“Coins”…dVersion)

game.Players.PlayerAdded:Connect(function(player)
local PD = save:GetAsync(player.UserId)

local coins 

if PD ~= nil  then
	coins = PD   
else
	coins = 0
	save:SetAsync(player.UserId,0)
end

local coinsValue = Instance,new("NumberValue", player)
coinsValue.Name = "Currency"
coinsValue.Value = coins

end)

game:BindToClose(function()
print (“STOPPED!”)

for i,player in pairs(game.Players:GetPlayer()) do
	local value = player.Currency.Value
	save:SetAsync(player.UserId, value)
	print("saved data for "..player.Name)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local value = player.Currency.Value
if value ~= nil then
print(“Found data to save for “…player.Name…”!”)
save:SetAsync(player.UserId, value)
print("saved data for "…player.Name)
else
print(“Did not manage to ind data to save for “…player.Name…”!”)
end
end)

It’s ‘GetPlayers()’ not ‘GetPlayer()’

Thanks for correcting me, even if it didn’t help.

Firstly, you aren’t changing the Currency value from the client, correct? If you are doing so, it won’t replicate to the server, thus causing the data to not save correctly.

Secondly, I noticed numerous errors within your script, which when I corrected, the script worked perfectly fine in Studio.
Here’s the corrected script:

local DSS = game:GetService("DataStoreService")

local dVersion = 1
local save = DSS:GetDataStore("Coins" .. dVersion)

game.Players.PlayerAdded:Connect(function(player)
	local success, response = pcall(
		save.GetAsync, save, player.UserId
	)
	
	if success then
		local coins 

		if response ~= nil  then
			coins = response   
		else
			coins = 0
			local success, response = pcall(
				save.SetAsync, save, player.UserId, 0
			)
			if not success then
				warn(response)
			end
		end

		local coinsValue = Instance.new("NumberValue")
		coinsValue.Name = "Currency"
		coinsValue.Value = coins
		coinsValue.Parent = player
	else
		warn(response)
	end
end)

game:BindToClose(function()
	print("STOPPED!")

	for _, player in pairs(game.Players:GetPlayers()) do
		local value = player.Currency.Value
		local success, response = pcall(
			save.SetAsync, save, player.UserId, value
		)

		if success then
			print("Saved data for " .. player.Name)
		else
			warn(response)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local value = player.Currency.Value
	print(value)
	if value ~= nil then
		print("Found data to save for " .. player.Name .. "!")
		
		local success, response = pcall(
			save.SetAsync, save, player.UserId, value
		)
		
		if success then
			print("Saved data for " .. player.Name)
		else
			warn(response)
		end
	else
		print("Did not manage to find data to save for " .. player.Name .. "!")
	end
end)

Hope this helps
Fizzitix

It really helped, thank you very much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.