ModuleScripts wont save

So I’m using ModuleScripts to save and load all of my datastores.
I have a function on a server script that detects when a user leaves and then saves the data in the module script…

This is my ModuleScript (save function)

function Arrest:SaveArrests(plr)
	local Data = DataStore:GetAsync(plr.UserId)
	DataStore:SetAsync(plr.UserId, Data)
end

This is my server function

Players.PlayerRemoving:connect(function(plr)
	ArrestModule:SaveArrests(plr)
end)

No errors, nothing. The data just doesn’t save.

First, a disclaimer: you should never have unprotected DataStore calls, as they can potentially fail and cause your script to error.

Onto the root of the problem, what your script is currently doing is:

When a player leaves the game, grab their data from the datastore, then set their data in the datastore to the data that we just grabbed from the datastore.

With this approach, no changes will ever be saved, because the data that you are saving is the data that was already in the datastore, not the data that was kept track of in the actual game.

I planned to add protection and what not (don’t worry, this is just a first quick attempt)

The “Data” that I called here

local Data = DataStore:GetAsync(plr.UserId)

is the data from the datastore?

Yes.

Yes… I know it is? So what do you mean.

You’re literally grabbing the data from the data store just to save it immediately without making any changes.

2 Likes

I fixed it by adding Session Data (as shown on the wiki)

I understood what you mean and you lead me to the solution.