Data stores not working in roblox studio

ive had this problem for a long time , but then it fixed itself for some reason. but after a while it came back, roblox studio not datastoring anything, literally nothing even a simple leaderstats datastore dosen’t work, i dont know what is wrong, when i join the game in Roblox the data stores work fine, but in roblox studio in particular it does not, the script is perfect and there is not problem with it, but ill just show it down anyways, even when i make a local server to test it, there is a 98% chance that it will not data store, and even if it did sometimes it just dosent load it after a couple of tries, sorry if this is long but ive been very irritated from this issue because it is vital to my game.

heres the script, any help will be appreciated:

local datastoreservice = game:GetService("DataStoreService")

local MoneyDataStore = datastoreservice:GetDataStore("MoneyData")

local players = game.Players

players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")

	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Cash = Instance.new("NumberValue")
	Cash.Parent = leaderstats
	Cash.Name = "Cash"

	local Bank = Instance.new("NumberValue")
	Bank.Parent = leaderstats
	Bank.Name = "Bank"


	local sucess, data, att = nil, nil, 1

	repeat
		sucess, data = pcall(function()
			return MoneyDataStore:GetAsync(plr.UserId)
		end)

		if not sucess then
			warn("Cannot load data for", plr.Name)
			task.wait(3)
		end
		att += 1
	until sucess or att == 5

	if sucess then


		if data then
			Cash.Value = data["Cash"]
			Bank.Value = data["Bank"]
		end
	else
		warn("Cannot load data")
		--plr:Kick("Cannot load your data, please retry later")
	end

end)

local function saveData(plr)
	
	local success, err, att = nil, nil, 1

	local data = {}

	repeat
		success, err = pcall(function()
			data = {
				["Bank"] = plr.leaderstats.Bank.Value;
				["Cash"] = plr.leaderstats.Cash.Value;
			}

			MoneyDataStore:SetAsync(plr.UserId, data)
		end)

		if not success then
			warn(err)
			task.wait(3)
		end
		att += 1
	until success or att == 5

	if success then

	else

		warn(err)
	end
end


game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	task.wait(1)
	for _, player in game.Players:GetPlayers() do
		task.spawn(function()
			saveData(player)
		end)
	end
end)

(API SERVICES ENABLED)

1 Like

You have to enable Studio Access to API Services in the game settings.

1 Like

i have it enabled, that’s why i came here because i couldn’t think of anything else that would cause this problem

1 Like

well in data stores do not work in studio. Really depends on this one sometimes they do sometimes they don’t. You will have to test them in game.

1 Like

thats sad, pretty sure there is a solution somehow, i cant just exit roblox studio and play on roblox EVERY SINGLE time i want to test it.

I can totally agree with you.

1.Are you sure that you are saving data correctly?
2. You could check how others do maybe that can help.

in your bind to close, put a task.wait(maybe 2 seconds) AFTER the data save function is called. In roblox itself it doesn’t really matter but in roblox studio the game can close “too fast” and the data won’t be saved. DataStoreService doesn’t run instantly obviously

1 Like

See if changing your code to this:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local MoneyDataStore = DataStoreService:GetDataStore("MoneyData")

local function onPlayerAdded(player)
	local success, data = xpcall(MoneyDataStore.GetAsync, warn, MoneyDataStore, player.UserId)

	if success then
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local Cash = Instance.new("NumberValue")
		Cash.Name = "Cash"

		local Bank = Instance.new("NumberValue")
		Bank.Name = "Bank"

		if data then
			Cash.Value = data.Cash
			Bank.Value = data.Bank
		end

		Cash.Parent = leaderstats
		Bank.Parent = leaderstats
	end
end

local function onPlayerRemoving(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		xpcall(MoneyDataStore.SetAsync, warn, MoneyDataStore, player.UserId, {
			Cash = leaderstats.Cash.Value,
			Bank = leaderstats.Bank.Value
		})
	end
end

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(1)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		local spawn = task.spawn
		local wait = task.wait

		local function onBindToClose(player)
			onPlayerRemoving(player)

			y += 1
		end

		for _, player in Players:GetPlayers() do
			x += 1

			spawn(onBindToClose, player)
		end

		repeat wait(0) until y == x
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

makes it work, although do note that my version doesn’t autosave. I edited it to match the names of the values in your version as closely as possible so you should be able to swap to it no problem. Do note that it will need to be a server Script in order to work, and also remember to change the values of Cash and Bank while play-testing in Server mode, not client mode else the values won’t replicate to the server and therefore the data will stay untouched. Also remember that you need to test DataStores in places that are published to Roblox, so testing in a place stored in a local file won’t work

1 Like