How do i make a separate loop for each player

Currently i am trying to make a tycoon like house tycoon where you get +1 cash per second except whenever a second player joins it basically adds another loop/doubling the amount every player is getting it should only be +1 per player or +1 + #OwnedPads

local function Increase(player, Tycoon, TycoonData)
	local Collector = Tycoon.Collector
	local gui = Collector.PrimaryPart:FindFirstChild("BillboardGui")
	local Currency = gui.Currency
	local OwnedPads = TycoonData.OwnedPads
	while true do
		TycoonData.Collector += 1 + #OwnedPads
		Currency.Text = "$"..TycoonData.Collector
		task.wait(1)
	end
end
Main.Load = function(player: Player, data)
	local CurrentTycoon = data.CurrentTycoon
	local TycoonData = data.TycoonData[CurrentTycoon]
	
	local ServerTycoon = ServerStorage.Tycoons:FindFirstChild(CurrentTycoon):FindFirstChild("Model"):Clone()
	ServerTycoon.Parent = ServerStorage.Tycoons:FindFirstChild(CurrentTycoon)
	local ReplicatedTycoon = ServerTycoon:FindFirstChild("Tycoon"):Clone()
	
	for _, v in workspace.TycoonSpawns:GetChildren() do
		if v:GetAttribute("Owner") == nil then
			v:SetAttribute("Owner", player.UserId)
			ReplicatedTycoon.Parent = v
			ReplicatedTycoon:SetAttribute("Owner", player.UserId)
			ServerTycoon:SetAttribute("Owner", player.UserId)
			ReplicatedTycoon:PivotTo(v.CFrame)
			ServerTycoon:PivotTo(v.CFrame)
			break
		end
	end
	
	local character = player.Character or player.CharacterAdded:Wait()
	character:PivotTo(ReplicatedTycoon.Spawn.CFrame)

	player.CharacterAdded:Connect(function()
		character:PivotTo(ReplicatedTycoon.Spawn.CFrame)
	end)
	
	GetOwnedBuilds(TycoonData, ServerTycoon, ReplicatedTycoon)
	
	for _, pad in ReplicatedTycoon:GetDescendants() do
		if pad:HasTag("Pad") then
			PurchasePad(pad, ServerTycoon, data, ReplicatedTycoon)
		end
	end
	
	ReplicatedTycoon.Pads.ChildAdded:Connect(function(pad)
		PurchasePad(pad, ServerTycoon, data, ReplicatedTycoon)
	end)
	
	local Collector = ReplicatedTycoon.Collector
	local gui = Collector.PrimaryPart:FindFirstChild("BillboardGui")
	local Currency = gui.Currency
	local DisplayName = gui.DisplayName
	local OriginalName = gui.OriginalName
	
	DisplayName.Text = player.DisplayName
	OriginalName.Text = player.Name
	Currency.Text = "$"..TycoonData.Collector	
	Collector.PrimaryPart.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end
		if player.UserId ~= ReplicatedTycoon:GetAttribute("Owner") then return end
		if CollectCooldown[player] then return end
		local Cash = player:FindFirstChild("Cash")
		
		Cash.Value += TycoonData.Collector
		TycoonData.Collector = 0
		Currency.Text = "$"..TycoonData.Collector
		ReplicatedStorage.Remotes.Notification:FireClient(player, nil, nil, "Collected")
		CollectCooldown[player] = true
		task.wait(1)
		CollectCooldown[player] = nil
	end)
	
	local HotPoint = ReplicatedTycoon:FindFirstChild("HotPoint")
	HotPoint.BillboardGui.PlayerName.Text = player.Name

	coroutine.wrap(function()
		Increase(player, ReplicatedTycoon, TycoonData)
	end)()
end

i dont see the problem with the scripts received, i assume that the problem is when calling the function Increase, i need the script that call this function to see the problem.

1 Like

i did include that part its at the bottom within the coruotine

nvm i ended up recreating the entire system with profile service and now it works perfectly

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