Adding to Players' Leaderstats

Hey there!
I have recently watched and used AlvinBlox’s shop gui tutorial, but I ran into a problem. I can’t figure out how to get it to add x amount of cash to every players’ stats every x amount of minutes. Every time I try, either an error occurs, or it doesn’t save the data. (Because I didn’t add in the code right. I have Studio access to API Services on, so that’s not the issue.) I’m stumped.
Here’s the script that I’ve tried most recently:

local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")



game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = folder
	
	local data
	local cashStored
	
	local success, errorMessage = pcall(function(plr)
		data = DataStore:GetAsync(plr.UserId.."tools")
		cashStored = DataStore:GetAsync(plr.UserId.."cash")
	end)
	
	if cashStored ~= nil then
		cash.Value = cashStored
	else
		cash.Value = 100
	end
	
	if data ~= nil then
		for _, toolName in pairs(data) do
			
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
			
			if tool then
				local NewTool = tool:Clone()
				NewTool.Parent = plr.Backpack
				
				local NewTool = tool:Clone()
				NewTool.Parent = plr.StarterGear
			end
			
			
		end
		
	end
	while true do --I added in this
		cash.Value = cashStored + 20
		wait(5)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsTable = {}
	
	for _, tool in pairs(plr.Backpack:GetChildren()) do
		
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
			
			table.insert(toolsTable, tool.Name)
			
		end
	end
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(plr.UserId.."tools",toolsTable)
		DataStore:SetAsync(plr.UserId.."cash",plr.leaderstats.Cash.Value)
	end)
	
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local toolsTable = {}

		for _, tool in pairs(plr.Backpack:GetChildren()) do

			if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then

				table.insert(toolsTable, tool.Name)

			end
		end

		local success, errorMessage = pcall(function()
			DataStore:SetAsync(plr.UserId,toolsTable)
		end)

	end
		
	
end)

Any and all help is much appreciated! Thank you so much.

1 Like

Try adding this to server script service:

    spawn(function()

    while true do
wait(60)
    for _,player in pairs(game.Players:GetChildren()) do
    player.leaderstats.nameofmoney.value += 50
    end
    end

    end)

It usually works for me

1 Like

It does add the value, however it does not save it. Thank you for the help, though! :smiley:

That code is no meant to save it your code automatically saves everything

1 Like

When I ran Studio, it worked, but I stopped and re tested, and it didn’t save the value that it left off at previously, I mean.

The saving should be done in your main data store script when the player leaves or in an auto save

1 Like

Yeah, that’s why I’m confused. It isn’t saving when I leave. Looks like another problem I have to solve. Anyways, thanks for solving my main post. I’ll mark it as a solution.

1 Like

Studio does not usually save properly it is not very reliable for testing datastores try using the actual game

2 Likes