Data store is not working

Hi, I have an issue with my data store and it prints yes for success and prints data saving however it does not show when I rejoin. I had a touch brick which gave me 10 Molla however when I rejoined, I had no Molla.

local myDatasStore = datastore:GetDataStore("myDataStore")-- In Quotes is name of data store

game.Players.PlayerAdded:Connect(function(p)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = p
	local money = Instance.new("IntValue")
	money.Name = "Molla"
	money.Parent = leaderstats
	
	local playerid = "Player_"..p.UserId
	print(playerid)
	
	myDatasStore:GetAsync(playerid) -- Get data
	
	--Load Data
	
	local Data = nil
	
	local success, errormessage = pcall(function()
		Data =  myDatasStore:GetAsync(playerid)---- Local for it to be shown for both functions
		return Data
	end)
	
	
	if success then -- if the data is found
		p.leaderstats.Molla.Value = Data
		print("yes")
	else
		print("rip")
		warn(errormessage)
		-- where data is loaded into the actual game
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local playerid = "Player_"..p.UserId
	
	print(playerid)
	local Data = p.leaderstats.Molla.Value --- Turning it into data
	
	local success, errormessage = pcall(function()
		myDatasStore:GetAsync(playerid, Data)-- saving the money -- Get the data already in game to save
	end)
	
	print("Passed")
	
	if success then
		print("Data saved")
	else
		print("no")
		warn(errormessage)
	end
	
end)
3 Likes

Howdy, how are you doing?
Are you sure you have this setting ON?

Yes I do have the setting on, is there any others I need ?

You’re not actually calling any methods on your datastore to save your information, only the GetAsync method which retrieves previously saved data. Please take a look at the following datastore methods to get a better idea of how you want to go about saving your game’s data

GlobalDataStore:SetAsync (roblox.com)
GlobalDataStore:UpdateAsync (roblox.com)

My advice would be to use UpdateAsync upon the Player’s removal.

This is the problem, you’re loading the data wrong.

Why are you loading the data twice? Load it once, so remove the first GetAsync() function code line.

The second is your pcall function. Since you have a variable to store the data, you don’t have to return the Data.

If that still doesn’t work, then you’re saving the data wrong.

This seems to be the error in this case then, you’re getting the async, not setting it.

change the “GetAsync” with “SetAsync”

This worked, thank you lots ////

No problem, though be sure to read the API too next time like @mew903 mentioned above with the links too. You could’ve solved this issue with a bit of searching.

This is not exactly a fix to the issue, but I thought it was good to note something:

You are creating a local variable Data and then setting it within the pcall function.
While this works, it is not advised to do it this way.
The pcall function actually sets what you have as errormessage to what you return within the pcall function.
You should make this code

local success, data = pcall(function()
	Data =  myDatasStore:GetAsync(playerid)---- Local for it to be shown for both functions
	return Data
end)

then data is either the save data or the error message
if success is true then data is equal to the save data
if success is false then data is equal to the error message.