Datastore problem

Hi.Hope this message find you in peace.

I am currently encountering this data store problem.After an hour of debugging, I still can’t find a possible solution to it.

local DataStoreService = game:GetService("DataStoreService")
local myData = DataStoreService:GetDataStore("CashData")


game.Players.PlayerAdded:Connect(function(player)
	
print("You have entered")
		local leaderstats = Instance.new("Folder",player)
		leaderstats.Name = "leaderstats"

		local Cash = Instance.new("NumberValue",leaderstats)
		Cash.Name = "CashValue"
	--[[Cash.Value = game.Workspace.tycoon.tycoonHouse.Dropper.Value.Value]]
	
	
print("Not leaderstats")	
		local UserID = player.UserId
		local data
	
print("Pcall starts!")
		local success, errorMessage = pcall(function()
	
			data = myData:GetAsync(UserID)
print("Hi")
		end)

		if success then
			data = Cash.Value
			print("Yes")
		else
			print(":(")
			warn(errorMessage)
		end

	end)

game.Players.PlayerRemoving:Connect(function(player)
	
	print("Entered")
	
	local UserID = player.UserId
  	
	local data = player.leaderstats.CashValue.Value
	print("leaved")
		local success, errorMessage = pcall(function()
			myData:SetAsync(UserID, data)
		end)
	
	print("Hehe")

		if success then
			print("DataSaved")
		else
			print("dataNotSaved")
			warn(errorMessage)
		end
		
end)

I have followed the everything in the data store tutorial.Every message in print("___") is printed as expected.I have commented ,disabled or deleted everything that will possibly affect the leaderstats. (I have a value that is responsible to change the number in the leaderstats.)However, I am still unable to receive the expected outcome.

Is there anything wrong with my code?Please help.Thank you.

I guess this should be reversed:

Cash.Value = data
1 Like

What? Cash is a NumberValue.

1 Like

Yeah sorry look at my edit on the first message

I read that completely wrong, just realised it’s not a variable :person_facepalming: you were right, sorry

1 Like

I thought it was a variable since I’m probably still half asleep sorry