I have recently been making a game where I need to store the player's money, that when the player is going to join next time he or she would have the same value of money as the last time the player played. I have tried hard to make it work but I can't solve the problem. Can you help me? I will remember to accept a fulfilling answer

--Variables--
trashClick = 0
trashValue = 0
local TrashPart = game.Workspace.TrashPart
local DataStoreService = game:GetService("DataStoreService")
local MoneyStore = DataStoreService:GetDataStore("MoneyStore")
local Data

game.Players.PlayerAdded:connect(function(player) -- This funcion starts to run when a player joins the game --

-- The leader board is about to be created--
	 local StartingMoney = (0)
	 local leaderstats = Instance.new("Folder")
	 leaderstats.Name = "leaderstats"
	 leaderstats.Parent = player
	 
	 local money = Instance.new("NumberValue")
	 money.Name = "Money"
	 money.Parent = leaderstats
	 money.Value = StartingMoney
	
	game.Workspace.TrashPart.ClickDetector.MouseClick:Connect(function() -- When a player clicks on a trash the funcion starts to run --
		trashValue = trashValue + 1
		trashClick = trashClick +1
		if trashClick == 10 then
			TrashPart:Destroy()
		end
		print(trashValue)
	end)
	
	game.Workspace.DumpingPart.Touched:Connect(function(player1) -- When a player touches the dumpster ground the function starts to tun --
		if player1.Parent:FindFirstChild("Humanoid") then
			money.Value = money.Value + trashValue
			trashValue = 0
		end
	end)
	
	-- Data store gets data from the player--
	local sucess, errormessage = pcall(function()
		Data = MoneyStore:SetAsync(player.UserId.."-money")
	end)
	if sucess then
		money.Value = Data
	else
		print("error")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)-- When a player quits the game the function starts to run --

-- Datastore saves the data --
	local sucess, errormessage = pcall(function()
		MoneyStore:SetAsync(player.UserId.."-money",player.leaderstats.money.Value)
	end)
	
	if success then
		print("data sotred")
	else
		print("error")
		warn(errormessage)
	end
	
end)

2 comments

1

You are using the wrong function on line 40. Get, not Set

0

I like how you use the datastore example from the roblox dev wiki but you may wanna read more on it before you just copy and paste code from the website: https://developer.roblox.com/en-us/articles/Data-store