Datastore not working Again

Hello, My Datastore won’t save i don’t know how but it doesn’t and it doesn’t give me any errors please help me is my roblox studio broken cause this is my 100 time that i posted that my Datastore won’t work.

----------------------------
--|Scripted By. TurteleDJ|--
----------------------------



--------------
--|Local's|--
--------------
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
--------------



--------------------
--|Getting Data|--
--------------------
game.Players.PlayerAdded:Connect(function(plr)
	
	local data = Instance.new("Folder") -- Creating a Folder To Store The Value's
	data.Name = "data"
	data.Parent = plr
	
	local Cash = Instance.new("IntValue") -- Creating The Cash Value
	Cash.Name = "Cash"
	Cash.Parent = data
	
	local Data
	local Succes, errormessage = pcall(function()
		Data = MyDataStore:GetAsync(plr.UserId.."-Cash", plr.data.Cash.Value)
	end)
	if Succes then
		Cash.Value = Data
	else
		print("There was an Error while getting "..plr.Name.."'s Data")
		warn("ERROR GEETTING DATA ERROR MESSAGE = "..errormessage)
	end
end)
--------------------



-----------------
--|Saving Data|--
-----------------
game.Players.PlayerRemoving:Connect(function(plr)
	local Succes, errormessage = pcall(function()
	MyDataStore:SetAsync(plr.UserId.."-Cash", plr.data.Cash.Value)
	end)
	
	if Succes then
		print("The Data From "..plr.Name.." Succesfully Saved!")
	else
		print("There whas an error saving the Data from "..plr.Name)
		warn("ERROR SAVING DATA ERROR MESSAGE = "..errormessage)
	end
end)
-----------------
1 Like

Hey there!

I just saw your posting and I have worked with Money Datastores quite a bit, I had to make sure you had everything set up properly though.

First thing: Datastores will load but can not be set in Studio, in my experience, :SetAsync() does not work within studio, you will have to test within the Roblox game itself.

Another thing, make sure you do have your Datastores on within the game settings, if they’re off you will get an HTTP 403 error code in your output. If you do get this error go into the game settings, go to Security, and then turn on “Enabled Studio Access to API Services”.

If all these things are in order and your script still does not work I did quickly write up a super basic Money Saving script that works (I did test it in a game), it is basic because I wrote it quickly but I hope it helps you reference and fixes your script.

   -- Test Datastore For DevForum

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("CashDataStore")
-- Local Variables Finished

players.PlayerAdded:Connect(function(plr)
	
	-- Player Data Folder
	local datafolder = Instance.new("Folder")
	datafolder.Name = "Data"
	datafolder.Parent = plr
	
	local CashVal = Instance.new("NumberValue", datafolder)
	CashVal.Name = "Cash"
	CashVal.Value = CashStore:GetAsync(plr.UserId, CashVal.Value) or 100 -- 100 preset money for new players
	
	while wait(120) do  -- repeats every 120 seconds
		CashVal.Value = CashVal.Value + 40 -- adds 40 dollars
	end
	
end)

players.PlayerRemoving:Connect(function(plr) -- player leaving
	print("Saving Money")
	local plrData = plr:FindFirstChild("Data")
	local CashVal = plrData:FindFirstChild("Cash")
	CashStore:SetAsync(plr.UserId, CashVal.Value) -- Setting async
end)

Hope this helps, have a good day!

2 Likes

I noticed the error you made immediately, and it’s not a error hard to make. Saving the data when there is no data will wipe out data. Therefore, if you jump in a game before immediately leaving, your data will be wiped clean.

Don’t panic, it’s easy to fix! Simply add a debounce to your data loading! Because this is player-specific, I’d recommend instancing a bool value into the Player when joined. Name it “FinishedLoading” and set the value to false. After you retrieve player data, set it to true.

When the player leaves the game, check player.FinishedLoading! If its value is false, do not save any data because it means the player has just joined and is now leaving, and player data has not loaded yet. This will significantly reduce the possibility of data wipes. Otherwise, your code seems fine!

Hopefully this helps!

2 Likes