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
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
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