Help with "In Pairs" only working for one model

So this works, but it only works for ONE of the models. Any tips on how to get it to work for ALL? Currently using “in pairs” to achieve this. This is a SERVER script.

for _, Tycoon in pairs (game.Workspace.OfficeTycoons:GetChildren()) do
	
	owner = Tycoon:WaitForChild("TycoonModel").OwnerName
	amountM = Tycoon.TycoonModel.StatCollector.AmountM.Value
	amountP = Tycoon.TycoonModel.StatCollector.AmountP.Value
	waitTime = Tycoon.TycoonModel.StatCollector.WaitTime.Value
	incrementM = Tycoon.TycoonModel.StatCollector.IncrementM.Value
	incrementP = Tycoon.TycoonModel.StatCollector.IncrementP.Value
	--This is the thing that holds the owners name
	moneyCost = 0
	ptsCost = 0
	prompt = Tycoon.TycoonModel.Claim.ProximityPrompt
	promptCollect = Tycoon.TycoonModel.StatCollector.Collect.ProximityPrompt

	prompt.Triggered:Connect(function(player)
		local char = player.Character
		local frame = player.PlayerGui:findFirstChild("CoreGui"):findFirstChild("Message")
		local text = frame:findFirstChild("Text")

		if (player:FindFirstChild("TycoonTiers"):FindFirstChild("Office").Value == true) then 
			print(player.Name.." already owns an Office!")
			text.Text = "You already own an Office!" 
			frame.Visible = true
			wait(3)
			frame.Visible = false
		else
			local stats = player:findFirstChild("playerStatus")
			if stats ~= nil then
				local cash = stats:findFirstChild("Money")
				local pts = stats:findFirstChild("PTS")
				if cash.Value >= moneyCost and pts.Value >= ptsCost then
					cash.Value  = cash.Value - moneyCost
					pts.Value  = pts.Value - ptsCost
					owner.Value = player.Name --save new owners name for later.
					player:FindFirstChild("TycoonTiers"):findFirstChild("Office").Value = true
					local own = player:FindFirstChild("TycoonTiers")
					own.Parent = player
					text.Text = "You now own an Office!" 
					print(player.Name.." now owns an Office!")
					frame.Visible = true
					Tycoon.TycoonModel:FindFirstChild("Claim"):Destroy()
					wait(3)
					frame.Visible = false
				else
					text.Text = "You don't have enough money!" 
					print(player.Name.." doesn't have enough money!")
					frame.Visible = true
					wait(3)
					frame.Visible = false
				end
			end
		end
	end)

	--while true do 
	--	if (Tycoon.TycoonModel.isSaved.Value == 1) then
	--		break
	--	end
	--	wait(1)
	--end

	promptCollect.Triggered:Connect(function(player)
		local char = player.Character
		local frame = player.PlayerGui:findFirstChild("CoreGui"):findFirstChild("Message")
		local text = frame:findFirstChild("Text")

		if player.Name == owner.Value and amountM ~= 0 and amountP ~= 0 then
			local stats = player:findFirstChild("playerStatus") --Find moneyholder
			local cash = stats:findFirstChild("Money")
			local pts = stats:findFirstChild("PTS")

			cash.Value = cash.Value + amountM
			pts.Value = pts.Value + amountP
			promptCollect.ActionText = "Money: $0 PTS: 0"
			promptCollect.Enabled = false
			wait(.1)
			promptCollect.Enabled = true
			amountM = 0
			amountP = 0
		else
		end
	end)


	local function Loop()
		if owner.Value ~= "" then
			amountM = amountM + incrementM
			amountP = amountP + incrementP
			promptCollect.ActionText = "Money: $"..addComma(amountM).." PTS: "..addComma(amountP)
			promptCollect.Enabled = false
			wait()
			promptCollect.Enabled = true
			wait(waitTime)
			print("Loop working")
			Loop()
		else
			amountM = 0
			amountP = 0
			promptCollect.ActionText = "Money: $"..addComma(amountM).." PTS: "..addComma(amountP)
			promptCollect.Enabled = false
			wait(waitTime)
			print("Loop waiting on player")
			Loop()
		end
		end
	Loop()
end

anyone out there have anything? I’m at a complete loss.

It’s because your loop function is running over and over again. You can fix this by wrapping it with a coroutine.

Any links to coroutines? Not familiar with this. Just trying to learn!

Here:

If you call your loop function at the bottom with coroutine.wrap(loop)() which creates a new coroutine and calls it at the same time. Effectively creating a new simulated thread, so it runs in parallel with the rest of your script.

Here is a link to coroutines: coroutine | Roblox Creator Documentation

1 Like

Can’t wait to get home and try this! This is new for me, I’m excited. Learn something new everyday! :muscle:t3::sunglasses:

1 Like

Do you really need to call “Loop” that frequently?

It’s to keep adding money to the players in house bank. Do you have a better way of writing it?

Just use a standard loop instead.

Try this code, maybe it will help.

	local function Loop()
		if owner.Value ~= "" then
			amountM = amountM + incrementM
			amountP = amountP + incrementP
			promptCollect.ActionText = "Money: $"..addComma(amountM).." PTS: "..addComma(amountP)
			promptCollect.Enabled = false
			wait()
			promptCollect.Enabled = true
			wait(waitTime)
			print("Loop working")
		else
			amountM = 0
			amountP = 0
			promptCollect.ActionText = "Money: $"..addComma(amountM).." PTS: "..addComma(amountP)
			promptCollect.Enabled = false
			wait(waitTime)
			print("Loop waiting on player")
		end
		while true do
			wait(.1)
			Loop()
		end
	end