DataStore script not working

why is my DataStore script not working even tho it seems like it doesn’t have errors:

-- Variables
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

-- Classic Leaderstats script lines 5-15
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "Cash"
	Cash.Value = 1

	-- DataStore Script lines 19-48		
	local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(playerUserId)  --Load Data
	end)

	if success then
		Cash.Value = data.Cash -- Set Player Data equal to currency in the game
	end	

end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {Cash = player.leaderstats.Cash.Value}


	local success, errormessage = pcall(function()
		DataStore1:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data Saved!")
	else
		print("There was an error while saving data")
		warn(errormessage)
	end

end)
1 Like

What exactly is the problem? Is it not saving, not loading, etc?

1 Like

sometimes studio datastore dosnt work sometimes try to add
bindtoclose with the same stuff in ur playerremoving

its not saving at all like its disabled, its loading i tried with putting print() everywhere but not saving

1 Like

i dont understand what is that?

I learned datastores a couple days ago and I had trouble with them too. I know you need to have the API enabled, just do as followsScreenshot (61)E
Then go to options and turn on the studio API services.

I hope I helped!

1 Like

oops the formatting is so bad, I am so sorry I am a newer member to the forum!

1 Like
if success then
		Cash.Value = data.Cash -- Set Player Data equal to currency in the game
	end	

You check for success but you aren’t checking for errors.

1 Like

thank you so much, but i already did that so that is not a problem

Always, I hope you find out what is wrong with it

1 Like

then please tell me how to do so

I am actually learning about anti exploit right now! I just started, but in a few days I could tell you more!

1 Like

I tried the script and gave me error
(ServerScriptService.Script:23: attempt to index nil with ‘Cash’)
So that mean if a new player joined without data. It won’t know what is data((.Cash))
to fix that just add

 if success then
    if data ~= nil then
    		Cash.Value = data.Cash 
    	end
    end
1 Like

oh cool ill try out if my scripts start working

nice, best of the luck in that!

1 Like

hi, script now works but it doesnt save the data, and there is no error, could you please help me out?

It did same for me, it worked only when I tested it with 2 players in.

Also worked when I tested it in game not in studio.

1 Like

well it doesnt work for me at all, actually it does work but seems to run after the game has been shut down

If you have API service enabled. Try it by spawning 2 Players.
2021-01-12 (6)

i do have API services on, tried on test server but still doesnt work