Help with money per second

Hello all! I am having a bit of trouble with finding out a way to give the player money per second based on how many workers they have :frowning: I honestly have no idea how to go about this so any help would be apreciated.

local Buttons = {}
local Team = script.Parent.SpawnLocation.TeamColor
local player = nil
local totalButtons = 1
local BuyParts = script.Parent.CargoContainer:GetChildren()
local Folders = {script.Parent.CargoContainer1, script.Parent.CargoContrainer1Shops}
local currentFolder = 1
local MPS = 0

game.Teams["Vorld Wision"].PlayerAdded:Connect(function(plr)
player = plr		

end)


function Buttons.Appear(mod)
	for _, v in pairs(mod:GetDescendants()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			if v.Name ~= "HumanoidRootPart" then
				v.Transparency = 0
				v.CanCollide = true
				v.CanTouch = true
			end
		
			
		end
		
		
		
	end
end

function Buttons.CheckIfCanBuy(Cost, plr)
	
	if (game.Players:GetPlayerFromCharacter(plr.Parent).TeamColor == script.Parent.SpawnLocation.TeamColor) and (game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value >= Cost) then
		game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value = game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value - Cost
		return true
	else
		return false
	end
		
end

function Buttons.FindOwner()
	return player
end

function Buttons.RevealButton(Folder) --the group of buttons you want to make appear
	
	for _, v in pairs(Folder:getChildren()) do
		for _, v in pairs(Folder:getDescendants()) do
			
		
			if v:IsA("Part") then
				v.Transparency = 0
				v.CanCollide = true
				v.CanTouch = true
				else if v:IsA("BillboardGui") then
					v.Enabled = true
				end

			end
		end
		
	end
	
	
	
end

local function MPSS()
	while true do
	
	task.wait(5)
		player:WaitForChild("leaderstats").Money.Value += MPS
	end
end


Buttons.Appear(Folders[1].ABuyButton) -- for testing purposes, this is just to spawn in the first button for the chain

Folders[currentFolder].ChildRemoved:Connect(function()
	if #Folders[currentFolder]:GetChildren() == 0  then
		currentFolder += 1
		Buttons.RevealButton(Folders[currentFolder])
		
	end
	print(Folders[currentFolder])
	print(currentFolder)
	if Folders[currentFolder] == script.Parent.CargoContrainer1Shops then
		MPS += 100
		print("MPS INCREASED")
	end
	
end)

local never = true

local thread = coroutine.wrap(MPSS)
thread()

return Buttons

This is the main script within every tycoon model. Near the bottom you can see that I attempted to use coroutine.wrap to try and run a loop that adds money for the player every couple of seconds without stopping the rest of the code. The issue is that when I update MPS, I don’t think the actual MPSS function is updating. Again, I’m open to all ideas so any help would be appreciated. (an explanation would also be appreciated)

You could just increase the players money each second and multiply it by the number of workers i don’t have my pc so i can’t give you a code right now, but i hope this can help

I’ll give a beginner version and an advanced version.
Beginner:

function GiveMoneyPerSecond()
   while task.wait(1) do
      for i, plr in pairs(game.Players:GetPlayers()) do
         plr.leaderstats.Money.Value += 1
      end
   end
end
task.spawn(GiveMoneyPerSecond)

Advanced:

function GiveMoneyPerSecond()
   while true do
      for i, plr in pairs(game.Players:GetPlayers()) do
         plr.leaderstats.Money.Value += 1
      end
      task.wait(1)
   end
end
local MoneyThread = coroutine.create(GiveMoneyPerSecond)
coroutine.resume(MoneyThread)

The main idea is to loop through all the players in the game per second and give them $1. However, we need to make it so that the script won’t be stuck in the while loop for infinity… We do this with coroutines or the spawn method. Coroutines, while more performance friendly, are more complicated then simply spawning the function. It’s up to you which one you want to pick depending on what you prioritise

thanks man I really appreciate it, Ill do my best to incorporate this!

Thanks for the guidance my guy! it 100% helps

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.