Data not being saved

Alright so today I was following one of AlvinBlox’s tutorials, and I came across the fact after I was done that the data didn’t save, and it Studio didn’t print any errors. What’s the problem?

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")




game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = leaderstats
	leaderstats.Parent = player
	
	local cash = Instance.nwe("IntValue")
	cash.Name= "Cash"
	cash.Parent = leaderstats
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
		
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error while retrieving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
		
	end)

	if success then
		print("Player Data Successfully Saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
		
	end
	
	
end)
2 Likes

spelt new wrong, it isn’t spelt “Instance.nwe” it’s spelt “Instance.new”

2 Likes

i did not read the fact that the output didnt print out any errors, usually i would expect it to print “attempt to index nil with nwe” Hope my solution worked for you. Also make sure to change the intvalue on the server and not the client

1 Like

Still not printing errors or working, thanks for point that out duh.

Note: I’m using the new beta output, but that shouldn’t change anything.

leaderstats.name was an error actually that I didn’t see, putting it in quotations and trying again…

@akitsuya It’s still not working yet.

1 Like

it should work after you fix the .Name for the leaderstats
any errors?

1 Like

Lol I was trying to delete another script and deleted that one. Fixing…

(That was the error)

1 Like

myDataStore:SetAsync(player.UserId…“-cash”,player.leaderstats.Cash.Value)

Do:

myDataStore:SetAsync(player.UserId…“-cash”, player.leaderstats.Cash.Value)

2 Likes

Alright now I’m just really confused. “Cash” doesn’t save data, and “Coins”, the one I was scripting, completely disappeared!

@ThoseNamesAreGood Okay I did that.

@SpiralGaia API Services is Enabled. Why would I rewrite it?

Nice Dominus that Dominus is my dream item rn

1 Like

When you define leaderstats and the name you need to put quotation marks leaderstats.Name = "leaderstats".

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStoreTest1")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."GameData")
	end)
	if success then
		if data ~= nil then
			cash.Value = data["Cash"]
			coins.Value = data["Coins"]
		end
		-- Set Data if Data Exists
	else
		print("There was an error while retrieving data")
		warn(errormessage)
	end -- Warn Failure
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player:FindFirstChild("leaderstats") then
		local data = {}
		data["Cash"] = player.leaderstats.Cash.Value
		data["Coins"] = player.leaderstats.Coins.Value
		
		local tries = 0	
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				myDataStore:SetAsync(player.UserId.."GameData", data)
			end)
			if not success then wait(1) end
		until tries == 5 or success
		if not success then
			warn("Cannot save data for player!")
		end
	end
end)

I rewrote everything, now saving both the cash and coins to one table. This is called a dictionary.
try this in a real game and see if your coins and cash are saved.

I made a function that retries saving your data 10 times if your data somehow doesn’t save.

1 Like

@DarkDanny04 I already fixed that issue.

1 Like

I see it retrying 5 times but that seems like more than enough so you don’t need to edit it. Testing now…

@Awesom3_Eric I put in the script and leaderboards don’t show up at all.

Recopy the code because I made a typo from your old script with the “nwe”

I didn’t see any edits to that post, and I searched for “nwe” in that pasted script and found nothing.

Datastore.rbxl (18.9 KB)

Publish this to your place and try it out

Do you have two scripts running? My script only instances one folder called leaderstats.

There is no leaderboard when I play that file

Yes, it was another script, that’s why I deleted the post.

Did you publish the file to a place and enable api accees?

1 Like