Running the same function at once?

So I have this dropper for my tycoon game and it spawns parts, but when a player touches them they fling. So I want to create a wait 20 for if the part still exists, then I will destroy it. The problem is I want another part to be spawned when the 20 seconds “wait” is still counting down. So I have to run the same function at once. How would I do that.

local serverStorage = game:GetService("ServerStorage")
local oresFolder = serverStorage:WaitForChild("Ores")
local drop = script.Parent.Drop
local dropperOresFolder = script.Parent.DropperOres
local billboardGui = serverStorage:WaitForChild("BillboardGui")

function addComma(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

local function dropOre(ore,price)
	local newOre = oresFolder[ore]:Clone()
	newOre.CFrame = drop.CFrame - Vector3.new(0,1,0)
	newOre:SetAttribute("Value",price)
	newOre.Parent = dropperOresFolder
	local newGui = billboardGui:Clone()
	newGui.PriceText.Text = "$"..addComma(price)
	newGui.Parent = newOre
	
	wait(20) -- heres the wait
	
	if newOre then
		newOre:Destroy()
	end
end

while task.wait(2) do
	local randomRarity = math.random(1,1000) -- simple test for now
	
	if randomRarity < 600 then
		dropOre("Stone",1)
	elseif randomRarity < 875 then
		dropOre("Coal",3)
	elseif randomRarity <= 925 then
		dropOre("Copper",8)
	elseif randomRarity <= 950 then
		dropOre("Silver",22)
	elseif randomRarity <= 970 then
		dropOre("Gold",58)
	elseif randomRarity <= 985 then
		dropOre("Crystal",135)
	elseif randomRarity <= 992 then
		dropOre("Emerald",325)
	elseif randomRarity <= 999 then
		dropOre("Ruby",1000)
	elseif randomRarity == 1000 then
		dropOre("Diamond",10000)
	end
end
1 Like

Maybe use a task.Spawn like this

local function dropOre(ore,price)
	task.spawn(function()
		local newOre = oresFolder[ore]:Clone()
		newOre.CFrame = drop.CFrame - Vector3.new(0,1,0)
		newOre:SetAttribute("Value",price)
		newOre.Parent = dropperOresFolder
		local newGui = billboardGui:Clone()
		newGui.PriceText.Text = "$"..addComma(price)
		newGui.Parent = newOre

		wait(20) -- heres the wait

		if newOre then
			newOre:Destroy()
		end
	end)
end

then you can call the function again without waiting for 20 secs

1 Like

Wouldn’t it be better to use the Debris service for this?

1 Like

He said, “I want to create a wait 20 for if the part still exists, then I will destroy it.”

You can but what if the part dont exist after 20 seconds, it may throw some error. You will need to use a pcall with the Debris service then

1 Like

So how would I do that?_______

1 Like

Just tried it out and it doesn’t throw any errors

1 Like

Up to you man, if you dont have additional condition to destroy the part and want to destroy the part only if its exist use Debris service.

I suggested the task.Spawn as you had a condition to destroy the part, so thought you might add some other condition, if needed.

Both ways are correct, use Debris service if you dont have any other Conditions or you can use task.Spawn

1 Like
local debrisService = game:GetService("Debris")

local function dropOre(ore,price)
	local newOre = oresFolder[ore]:Clone()
	newOre.CFrame = drop.CFrame - Vector3.new(0,1,0)
	newOre:SetAttribute("Value",price)
	newOre.Parent = dropperOresFolder
	local newGui = billboardGui:Clone()
	newGui.PriceText.Text = "$"..addComma(price)
	newGui.Parent = newOre
	
	debrisService:AddItem(newOre, 20) --schedules for deletion to occur after 20 seconds
end

This is better than task.spawn.

1 Like

It is and i would have suggested that too but if he had his code like this

wait(20)
part:Destroy()

But he had a if condition, So that’s why suggested to use task.Spawn.

1 Like

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