Cannot save data for datastore propertly

Hello. I have an issue - when i try to save a data with datastore, it wont work. Code ends at :SetAsync function (when i tested it print just stopped at :SetAsync at pcall). Any help is great for me!

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local database = dataStoreService:GetDataStore("data")
local sessionData = {}

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local dollars = Instance.new("IntValue")
	dollars.Name = "Dollars"
	dollars.Parent = leaderstats
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to database")
		if not playerData then
			print("Assigning default data")
			playerData = {
				["Dollars"] = 15,
				["Cars"] = {"Ferrari", "Bugatti"}
			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable for get data for", player.UserId)
		player:Kick("There is an error while loading your data. Rejoin soon")
	end
	
	dollars.Value = sessionData[player.UserId].Dollars
	
	dollars.Changed:Connect(function()
		sessionData[player.UserId].Dollars = dollars.Value
	end)
end

players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)
	print(sessionData)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		print(attempt)
		
		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			attempt += 1
			warn("Errors while saving: "..errorMsg)
			if not success then
				warn(errorMsg)
				task.wait(3)
			else
				--print("DDHSHODD")
			end
		until success or attempt == 5
		
		print("Good")
		
		if success then
			print("Data saved successfully for ", player.Name)
		else
			print("Unable to save data for", player.Name)
		end
	end
end

players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	print("Server shutting down...")
	for i,player in ipairs(players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)
5 Likes

is the game public? if not, then that may be the problem.

6 Likes

i will try to make it public soon. Im not home rn

5 Likes

Didnt worked. No result at all

3 Likes

It works only when i put warn(errorMsg) into the repeat

3 Likes

It doesnt work with warn too. Idk what to do

3 Likes

Are you getting any errors or ‘errorMsg’ messages in your output?

2 Likes

no. I got them like 5 minutes ago, but rn no, idk what was changed, but it doesnt work. It stops on pcall (:SetAsync) i think, bc last print i put in code worked before :SetAsync, after no.

2 Likes

Try checking if ‘Enable Studio Access to API Services’ is enabled. File > Game Settings > Security. I’ve just ran this script in my sandbox and it works fine. Give things a couple of minutes after enabling before play testing as it occasionally takes a couple to propagate.

4 Likes

It is enabled. And the game is public

2 Likes

I gave a time. Doesnt work lol

2 Likes

Where is your script within your explorer? Can you screenshot your output?

2 Likes


image

It is located in serverscriptstorage.

3 Likes

This shows the script is working. What isn’t working for you?

4 Likes

If it is working, it should print “Data is successfully saved for someone”. And when i change dollars value and then leave or shutdown a server, the money is still 15, but i set 100.

3 Likes

Maybe this is a roblox studio problem. Lemme join into the game not in studio and check it

3 Likes

It is the studio error. It works fine in a game without studio. I dont know why. How do i report that bug into roblox?

3 Likes

its not bug, thats just how save data works, it there in case the developer wants to play the game and not accidently edit their save data in studio

4 Likes

How to disable this? I want to test that in studio

2 Likes

you cant, maybe you can if you play the game in roblox first then studio i think i remember it working like that, cuz if you play the game then you have a datastore but if you dont then it cant, idk

2 Likes