My data store script doesn't work

When I exit the game it always warns Couldn’t Save - Server - DataStore:16
Here is my code:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	local cash = Instance.new("IntValue", player)
	cash.Name = "Cash"
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormsg = pcall(function()
		playerData:SetAsync(player.UserId.."-cash", player.Cash.Value)
	end)
	if success then
		warn("Saved Successfully")
	else
		warn("Couldn't Save")
	end
end)
4 Likes

This is the problem i’ve been getting a lot. The reason is that in test mode. You’re the only person there. Which means if you left the server will just closed which makes the saving closed as well. To prevent this you’ll need to use the Bind to close function. You can get more information at the link i’ve given you.

1 Like

The first argument of SetAsync is the key on which you are saving the data. playerData is the datastore you are saving it to, so you cant use it as a key. You should use player.UserId as a key.

1 Like

Well I have tried that but now it doesn’t warn anything there is nothing in the output
no success or anything

Can you show the script after the changes?

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	local cash = Instance.new("IntValue", player)
	cash.Name = "Cash"
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormsg = pcall(function()
		playerData:SetAsync(player.UserId.."-cash", player.Cash.Value)
	end)
	if success then
		warn("Saved Successfully")
	else
		warn("Couldn't Save")
	end
end)

It should definetly work now, maybe the problem now is that server closes before it could save the data like @bookgamery555gta said, in that case bindtoclose should help.

But i tested it one time in a different script and it worked it did say saved successfully
But now it doesn’t

Hello, the problem here is most likely because you didn’t load your player’s data when they joined the game; this triggers an error whenever you want to save the player’s data when they join because there was no data that was loaded in the first place.

The best way to go about this is to use this script I’ve made, this is probably one of the most basic uses of loading and saving player data:

local DataStoreService = game:GetService("DataStoreService")
local CashDataStore = DataStoreService:GetDataStore("CashDataStore")

game.Players.PlayerAdded:Connect(function(Player)
    
    local leaderstats = Instance.new("Folder",Player)
    leaderstats.Name = "leaderstats" -- you can only name this 'leaderstats'.

    local Cash = Instance.new("Intvalue",leaderstats)
    Cash.Name = "Cash"

    local UserId = Player.UserId
    local Data = nil

    local s, e = pcall(function()
        Data = CashDataStore:GetAsync(UserId) -- loads data
    end

    if Data ~= then -- if the player's data is not nil (meaning this isnt their first time playing) then it will load the player's cash data.
        Cash.Value = Data
        print("Loaded Data")
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local UserId = Player.UserId
    local Data = Player.leaderstats.Cash.Value

    local s, e = pcall(function()
        CashDataStore:SetAsync(UserId, Data) -- saves data to the player thats leaving
    end

    if s then
        print("Saved Data")
    else
        warn("Error saving data")
    end

end)

Hope this helped!

Here is a datastore video that might help you

I’m sorry but what do you mean by that?? Sorry lol

The script didn’t work
Char limit just means that i can’t post my reply because of character limit

Can you also make it print the errormsg? It would help with finding out the error.

	if success then
		warn("Saved Successfully")
	else
		warn("Couldn't Save. Error: " .. errormsg)
	end

Wait a second after I did that it warned “Saved Successfully” why is that?
Is my script broken?

Why is the error message says successfully saved? am i stupid?
Did i do something wrong?

You possibly did something that made the script work.

I just copied what you said here
Can you also make it print the errormsg ? It would help with finding out the error.

where do you test? If in studio,you should access data store to save data while in studio

And how do I do that? char limittttt

You know what I will just test it in the actual game