Datastore doesn't save

Hello. The datastore saves when a player buys currency with robux but it doesn’t save when a player wins a round. I don’t know why this is happening.

Datastore Script (from this):

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")

local function saveData(player)

	local tableToSave = {
		player.leaderstats.Essence.Value;
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player)

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

	local Essence = Instance.new("IntValue")
	Essence.Name = "Essence"
	Essence.Parent = leaderstats

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then

		Essence.Value = data[1]

	else
		print("The player has no data!")
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player)
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Script that gives the player currency after winning (if it’s important)

game.Players.LocalPlayer.leaderstats.Essence.Value += workspace.Values.CoinsValue.Value

Thanks for helping

1 Like

To save you from all that headache of debugging, you can try my script, it saves multiple stats automatically without needing to code anything, here it is: https://devforum.roblox.com/t/i-made-a-saving-stats-script-that-is-fully-configurable-and-supports-multiple-stats/1939951

If you’d rather debug it, tell me and i’ll try helping!

You can’t save datastore values on the client. Get the player object and save it from the server. If you really want it to run on the client, use a RemoteEvent to tell the server to save the value.
Also, why is the Values folder parented to the Workspace if they are numbervalues? It’s way more organised if you put it in ReplicatedStorage.

The reason the DataStore script isn’t saving the data when a player wins a round is because there is no code in the script that updates the value of Essence when the player wins.

You’ll need to modify the script that gives the player currency after winning to also update the value of Essence and save it to the DataStore.

Here’s an example of how you can modify the script to update Essence and save it to the DataStore:

game.Workspace.Values.CoinsValue.Changed:Connect(function(newValue)
    if newValue == 10 then -- Change this to the value that triggers a win
        local player = game.Players.LocalPlayer
        player.leaderstats.Essence.Value = player.leaderstats.Essence.Value + 1 -- Change this to the amount of Essence the player should receive when they win
        saveData(player) -- Call the saveData function to save the updated value to the DataStore
    end
end)

Note that you’ll need to replace the 10 with the value that triggers a win in your game, and replace the 1 with the amount of Essence the player should receive when they win.

Also, make sure to add the saveData function to this script, or require the DataStore script that contains it.

Can you give me an example of telling the server to save the value?

And should I change something in the leaderstats script or the script that gives players currency?

Sure. You’re going to need a script and a LocalScript.
First, create a remoteEvent somewhere inside ReplicatedStorage
LocalScript:

-- we need to tell the server to change the player's leaderstat value
-- so, we will use a remote event to communicate this to the server.
local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote:FireServer(currencychange)
-- Remember, when using the FireServer method, we don't need to specify the player argument as it is already sent for us!

Script

local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote.OnServerEvent:Connect(function(player, currencychange)
    player:WaitForChild("leaderstats").Essence.Value += currencychange
end)
-- this is when we recieve the message from the client saying that they want their essence value changed.
-- remember, when we recieve a message from the client, we need to get WHICH client
-- so, we need to pass the player parameter into the Connect Function part.

If you need me to explain a bit more, let me know!

Didn’t work.

I got this error in output: ServerScriptService.EssenceCollection:3: attempt to perform arithmetic (add) on number and nil

Maybe I should explain how the game works?

it measn that the Essence.Value is nil.
Easy fix - we just check if its nil, and if it is, we set it to 0

local remote = game:GetService("ReplicatedStorage").RemoteEvent
remote.OnServerEvent:Connect(function(player, currencychange)
    local essence = player:WaitForChild("leaderstats").Essence
    if essence.Value == nil then
        essence.Value = 0
    essence.Value += currencychange
end)
-- this is when we recieve the message from the client saying that they want their essence value changed.
-- remember, when we recieve a message from the client, we need to get WHICH client
-- so, we need to pass the player parameter into the Connect Function part.

Or, its that you didn’t provide a value for CurrencyChange when you called :FireServer

All this time I was putting a value and it didn’t work. But when I enter a number it works.

CoinsValue has a value of how much the player survived and how much currency they get.

Works:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

remote.OnServerEvent:Connect(function(player, currencychange)
    player:WaitForChild("leaderstats").Essence.Value += 15 -- Works
end)

Doesn’t work:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

remote.OnServerEvent:Connect(function(player, currencychange)
    player:WaitForChild("leaderstats").Essence.Value += workspace.Values.CoinsValue.Value -- Doesn't work
end)

yeah the reason why is because workspace.Values.CoinsValue.Value is most likely nil.
Also, you are meant to change Essence.Value by currency change, that why its passed by the remote event…

It worked. Thank you for helping.

Here, you’re using LocalPlayer (I assume it’s LocalScript), so now whenever you do something in LocalScript, it doesb’t replicate to server and DataStores save it from server.

Bruh, why I replied, it is already solved.

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