How to make a money gui that updates because of how long the player has been playing

Hello I’m looking for a way to make a in game currency that increases depending on the time I would like a brief explanation and maybe a script (just explain the script a bit to please) I’m more of a builder so I can’t script.

game.Players.PlayerAdded:Connect(function(player)
	repeat wait(1) -- change 1 to seconds between payout
		if player then
			-- add money
		end
	until not player
end)

thanks for the reply i’ll try it out

in --add money what do you mean?

where do i put it to in serverscriptservice or somewhere else

do i do this player.leaderstats.Cash.Value +1

First you need a script that creates “leaderstats” folder inside of a player, and a value (or values) inside of this folder. Do you have it?

um i can’t script that script i got was from a game i was a builder in

In ServerScriptService.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

function manageData(player,instruction,data)
	local MoneyStore = DataStoreService:GetDataStore("Money")
	local Money
	if instruction == "fetch" then
		local data = MoneyStore:GetAsync(player.UserId)
		if data then
			Money = data
		else
			Money = 0
			MoneyStore:SetAsync(player.UserId,Money)
		end
		local numCheck = tonumber(Money)
		if not numCheck then
			Money = 0
			MoneyStore:SetAsync(player.UserId,Money)
		end
		return Money
	elseif instruction == "save" then
		MoneyStore:SetAsync(player.UserId,Money)
	end
end

Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local money = Instance.new("IntValue",leaderstats)
	money.Name = "Money"

	local success,err = xpcall(function()
		local data = manageData(player,"fetch")
		if data then
			money.Value = data
			coroutine.resume(coroutine.create(function()
				while wait(1) do
					if player then
						money.Value += 1
					end
				end
			end))
		end
	end, function()
		warn("There was an error fetching data for "..player.Name..".")
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local success,err = xpcall(function()
		manageData(player,"save",player.leaderstats.Money.Value)
	end, function()
		warn("There was an error saving data for "..player.Name..".")
	end)
end)

This script is not good, you should not put it in the ServerScriptService, @mrrtgn try to fire your game on 2 or more clients and see what will happen

Edit: with @7rippy script ofc

That should do it, I haven’t tested so lmk.

Fatal mistake, have you ever wondered why everyone is shouting that DataStoreService operations should be secured by pcall()?

Edit: Also you didn’t close the brackets

where should i put the pcall? maybe before or after it maybe like
local success, response = pcall(
Source Pcalls - When and how to use them

I will view the replies later just got to do a thing

Already implemented into script, see revision. :slight_smile:

k i’ll check it out later! thanks for the replies

The script didn’t work? Is it my leaderstats script?

Try looking into RenderStepped/RunService So on every 100th frame it fires a function that would change the player’s currency value with a pop-up gui saying “Thanks for playing, here’s a bunch of money”. or… just do something like this:

Server script> ServerScriptService

while true do
game.RepStorage.GiveMoneyReward:FireAllClients()
end

local script > StarterGui

game.ReplicatedStorage.GiveMoneyReward.OnClientEvent:Connect(function()
give money
end

This would crash the game. Remotes should never be placed on a while true loop. It also wouldn’t do anything because it would only replicate to the client. Not to mention handling data such as cash from the client would allow exploiters to edit it at free will.

Updated and works. @mrrtgn

--	// Service(s)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--	// Variable(s)
local waitTime = 1 -- seconds between payout(s)
local payAmount = 1 -- yes

--	// Function(s)
function manageData(player,instruction,data)
	local MoneyStore = DataStoreService:GetDataStore("Money")
	local Money

	if instruction == "fetch" then
		local data = MoneyStore:GetAsync(player.UserId)
		if data then
			Money = data
		else
			Money = 0
			MoneyStore:SetAsync(player.UserId,tonumber(Money))
		end

		local numCheck = tonumber(Money)

		if not numCheck then
			Money = 0
			MoneyStore:SetAsync(player.UserId,tonumber(Money))
		end

		return Money
	elseif instruction == "save" then
		MoneyStore:SetAsync(player.UserId,tonumber(data))
	end
end

--	// Signal(s)
Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local money = Instance.new("IntValue",leaderstats)
	money.Name = "Money"

	local success,err = pcall(function()

		local data = manageData(player,"fetch")

		if data then
			money.Value = data
			coroutine.resume(coroutine.create(function()
				while wait(waitTime) do
					if player then
						money.Value += payAmount
					end
				end
			end))
		end
	end)
	
	if success then
		return;
	else
		warn("Encountered an error while fetching data for "..player.Name..".")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success,err = pcall(function()
		manageData(player,"save",player.leaderstats.Money.Value)
	end)
	
	if success then
		return;
	else
		warn("Encountered an error while saving data for "..player.Name..".")
	end
end)