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

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)

this didn’t work but it did give me leaderstats

@7rippy all your scripts are full of issues

Please let me do that

Script in ServerScriptService:

local CashDataStore = game:GetService('DataStoreService'):GetDataStore('Cash')
local TimeBetweenPayout = 20
local PayoutValue = 5

game:GetService('Players').PlayerAdded:Connect(function(player)
    local ID = player.UserId
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player
    
    local cash = Instance.new('IntValue')
    cash.Name = 'Cash'
    
    local succes, errorMessage = pcall(function()
        cash.Value = CashDataStore:GetAsync(ID)
    end)
    
    if succes then
        print('Data loaded ✓')
    else
        warn(errorMessage)
    end
    
    local cashNow
    
    cash.Changed:Connect(function()
        cashNow = cash.Value
    end)
    
    while task.wait(TimeBetweenPayout) and player do
        cash.Value += PayoutValue
    end
    
    local succes, errorMessage = pcall(function()
        CashDataStore:UpdateAsync(ID, cashNow)
    end)
    if succes then
        print('Data updated ✓')
    else
        warn(errorMessage)
    end
end)

I wrote that script on mobile device so please watch the output and report any possible issues

player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1

Works completely fine for me so idk. The one you provided does not work at all.

Your code is messy and not safe, xpcall() does not yield

Organization depends on coding style, but you’re correct on the pcall. I changed it.