How do you run mutiple functions with waits at the same time without affect eachother?

  1. I’m trying to make a tycoon and make the droppers work in one script using a function and a for loop. The script does run the function but only for one “producer”.

-- module
local TycoonModule = require(script.Parent.TycoonModule)

-- variables
local Tycoon = script.Parent
local StartingProducers = Tycoon.StartingItems.Producers
local ClaimedEvent = Tycoon.ClaimedEvent
-- function

function ProducerScript(Producer, Owner, OwnerData)
	local ResourceProduced = Producer:FindFirstChild("ResourceProduced").Value
	local ProducedAmount = Producer:FindFirstChild("ProducedAmount").Value
	local OwnerResource = OwnerData:FindFirstChild(ResourceProduced)

	while Producer:GetAttribute("Active") do
		OwnerResource.Value = OwnerResource.Value + ProducedAmount
		wait(Producer:GetAttribute("IntervalTime"))
	end
end

ClaimedEvent.Event:Connect(function()
	for index, producer in pairs(StartingProducers:GetChildren()) do
		producer:SetAttribute("Active", true)
		
		task.spawn(ProducerScript(producer, TycoonModule.TycoonOwner, TycoonModule.TycoonOwnerData))
	end
end)

  1. I’ve asked ai and looked around but cant seem to find anything, please help!
2 Likes

You’ve got it mostly correct. The only thing that is wrong is how you are passing your arguments in task.spawn

When you are doing ProducerScript(…), you’re causing lua to run that function and put the result inside task.spawn. But since it never ends it permanently yields. You need to pass the function in rather than call it

task.spawn(ProducerScript)

Though, your function requires parameters. task.spawn actually takes any extra parameters and plugs them into the function that is the first parameter.

So replacing your task.spawn line with

task.spawn(ProducerScript, producer, TycoonModule.TycoonOwner, TycoonModule.TycoonOwnerData)

should make it work

2 Likes

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