How would I save a Number Value to a data store?

function SafeSave(Datastore, Key, Value)
	local Attempts = 0
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Datastore:SetAsync(Key, Value)
		end)
		if not Success then wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then print("<Error while saving data>\n    - "..(Error or "No Error")) return nil end
	return Success
end


function SafeGet(Datastore, Key)
	local Attempts = 0
	local Data = nil
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Data = Datastore:GetAsync(Key)
		end)
		if not Success then wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then print("<Error while retrieving data>\n    - "..(Error or "No Error")) return nil end
	return Data
end


function SafeUpdate(Datastore, Key, Logic)
	local Attempts = 0
	local Data = nil
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Data = Datastore:UpdateAsnyc(Key, Logic)
		end)
		if not Success then wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then print("<Error while updating data>\n    - "..(Error or "No Error")) return nil end
	return Data
end


local LevelsDataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
	local levels = Instance.new("Folder")
	levels.Name = "Levels"
	levels.Parent = player


	local Level1 = Instance.new("NumberValue")
	Level1.Name = "Level1"
	Level1.Parent = levels


	Level1.Value = SafeGet(LevelsDataStore, tostring(player.UserId).."-Levels") or 0
end)


game.Players.PlayerRemoving:Connect(function(player)
	SafeSave(LevelsDataStore, tostring(player.UserId).."-Levels", player.Levels.Level1.Value)
end)
1 Like

Do I put this in a module script or a server script?

1 Like

You put this in a server script not a module script

2 Likes

So I scripted the entire script and there is a red line under the comma on the last line of the code.

1 Like

You did not end the line of code with a parentheses.

Where am I suppose to put the parentheses?

You have to put the parentheses after the player.Levels.Level1.Value because you are passing arguments to the function I presume. Tell me if it is still underlined.

Yeah, it’s still underlined in red.

Ok I might be guessing but add another parentheses after the “Levels” with the parentheses at the end.

Oops I added an extra ‘)’ by accident
try this:

game.Players.PlayerRemoving:Connect(function(player)
	SafeSave(LevelsDataStore, tostring(player.UserId).."-Levels", player.Levels.Level1.Value)
end)
2 Likes

Lol what was I thinking about! Well Dogcraft looks like you have your answer!

Surprisingly, the script did not work and the numbervalue didn’t save. There were no errors and API service was on.

What did you use to modify your numbervalue, you might have modified it on the client only, causing it to not save

edit:

I just copy and pasted the script into a place and it works fine

Why is it working for you but not me? Cause I just went into a different place, copied and pasted the code, made a server script, and turned on API service, and it doesn’t save when I change the value.

Are you changing it on the server or the client

this guy had just changed it on the client:

make sure you change the value on the server and check for errors

I made it on the server side, and there were no errors. So I don’t know about whats going on.

Invite me to studio and I will see, cause if it works for me there is something wrong with your place or your setup

image

I need to add you on Roblox because Roblox only allows people on your friend list to be in studio together.

You were changing it from the client, post is solved?

oh yeah I need to do that. Thanks for reminding me.