Save Data Problem

My destination is creating a data store for a value named Skoupes. However, while it works in the Studio, and not exactly as expected because it doesn’t save at certain times it doesn’t even save in the Roblox Player .
I have tried to look up for any errors in the output, but it seemed like it printed as expected. So I believe that I didn’t script it like I was aiming for. Since I am not familiar with datastores and it’s my weak side I have decided to look up for any help in the devforum. Can someone help me? I would appreciate it.

local DataStoreService = game:GetService("DataStoreService")
local MyData = DataStoreService:GetDataStore("Skoypes")

game.Players.PlayerAdded:Connect(function(plr)
	local skoypes = Instance.new("NumberValue")
	skoypes.Parent = plr
	skoypes.Name = "Skoypes"

	
    local success, errormsg = pcall(function()
		local data = MyData:GetAsync(plr.UserId)
		if data then
			skoypes.Value = data
			print("good")
			else 
			print("no data to be stored")
		end
	end)
     if errormsg then
	  warn(errormsg)
     end
	 
	 if skoypes.Value > 0 then
			plr.Character.HumanoidRootPart.CFrame = game.Workspace.SkoupesFile.CsSpawn.CFrame
	 end
	end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
		MyData:SetAsync(plr.UserId, plr.Skoypes.Value)
	end)
	if success then
		print("Good while saving the data")
		else
		warn(errormsg)
	end
end)
1 Like

I recommend using

MyData:UpdateAsync(function(plr.UserId)
return plr.Scopes.Value
end

As :SetAsync can cause data loss very frequently

1 Like

Maybe that’s he case. I have recently heard that UpdateAsync is better than SetAsync. I’ll try it and update you.

2 Likes

Oh and also for the data not saving in roblox create a function that will save the player’s data and then call that function with

game.Players.PlayerAdded:Connect(saveFunctionName)

And then also call it with

game:BindToClose:Connect(function()
for i, plr in game.Players:GetPlayers() do
saveFunctionName(plr)
end
end)

This will save all player’s data when the server shuts down

1 Like

Does :SetAsync() really cause data loss frequently? Do you think this is the solution to my datastore problem post?

1 Like

It prints an error. Expected “)” to close at column 28 got, got “.”

local function SaveData(plr)
MyData:UpdateAsync(function(plr.UserId)
	return plr.Scopes.Value
   end)
end
1 Like

That means there’s a ) missing somewhere check inside your script if you see any red lines

1 Like

The red line you referred to above is in the UpdateAsync function plr.UserId “.”

Could you show a screenshot of your script and the output please ?

Yes, here is the screenshot of my script and the script analysis.

Ahh yes my bad i remembered wrong it’s :

MyData:UpdateAsync(plr.UserId, function()

1 Like

I think it is like that
game:BindToClose(SaveData)

No this will call the function but no players will be passed in it so it will error

Related to that when I tried your code It printed some errors.

game:BindToClose:Connect(function()
for i, plr in game.Players:GetPlayers() do
saveFunctionName(plr)
end
end)

If you truly care about persistent data which is reliable, you’re better off just using ProfileService. With automatic backups and versioning, you avoid mistakes like inserting non-primitives and corrupting data.

Oh, it’s
game.BindToClose the previous was game:BindToClose

1 Like

Oh yes i was wrong again, sorry for that

Yes this should work, try it out!

Wait you forgot to add

game.Players.PlayerRemoving:Connect(SaveData)

It’s

game:BindToClose(function()

There’s the : and there’s no Connect it’s pretty weird that’s why i got it wrong