Basically, I am making a mining game. I already have the generation of the nods and the detection when the nods get destroyed.
I want to make my nods spawn again in the workspace, at the same spot, after 60 seconds.
I have no clue how I can like handle all the timers for the regeneration of different nods in ONE single script.
--My Script that will handle the respawning of the nods
Respawn_Nod_Event.Event:Connect(function()
end)
--My script that lunches the Bindable Event when the Nods gets destroyed
if New_Nod_Durability <= 0 then
local Chosen_Gem = Return_Chosen_Gem_Event:Invoke(plr, Nod)
print(Chosen_Gem)
local Cloned_Chosen_Gem = Gems_Folder_Table[table.find(Gems_Folder_Table, Chosen_Gem)]:Clone()
Cloned_Chosen_Gem.CFrame = Nod.CFrame
Cloned_Chosen_Gem.Parent = workspace.Gems
Respawn_Nod_Event:Fire(Nod)
Nod:Destroy()
end
How exactly do you want this regeneration system to work?
If you want the exact same node to respawn after being destroyed, then make a clone of it in your event function, wait a bit, then parent the clone to the workspace.
If you want a random node to spawn after a node is destroyed, you can save the position of the destroyed node, get a new random node, then place that new node at the saved position.
My biggest issue is how to handle the timer/cooldown of each nod to respawn. I want to handle like the waiting time in one single server script. I have a brief idea of how to respawn it, but idk how to make the timer though.
Do you want each node to have a different cooldown? If so, you can create a ModuleScript to act as a Cooldown Table, where you just create a new entry with the name of the node as the Key and the cooldown time as the Value.
When you go to respawn a node, check that table for a value that corresponds to the node. If there is a value, wait for that amount of time, otherwise wait for some backup amount of time.
You can use multiple threads to do this in combination with RBXScriptSignal which would be the best method imo since you wouldn’t have to constantly check the health of the gem in a while true do loop. Anyway, first what you need to do is create a loop that will run for each of the possible gem locations, then for each of the gems’ locations, listen for the gem’s health to change (which is often done by a NumberValue, attribute or some like method). When it changes, check if the health is less than 0, if the health is less than 0, destroy the gem. Then, wait the amount of time before the gem regenerates and then handle the respawn. This would really be easily done if you were working with object-oriented programming but really a loop should work.
for i,v in ipairs(gemsLocations) do
-- I'm assuming `v` is equal to a CFrame
coroutine.wrap(function() -- creates a new thread so we can handle multiple asynchronous events simultaneously
while true do
local newGem = getGem() -- this function should return your new gem
local health = gem:GetAttribute('Health') -- this will be a NumberValue and contains the health of the gem
local attributeChangedSignal = gem:GetAttributeChangedSignal('Health') -- this will be an RBXScriptSignal, we want to save it to a variable so the function isn't repeatedly called
while gem:GetAttribute('Health') > 0 do
attributeChangedSignal:Wait() -- will yield this thread until the health is changed
-- ^ we also are using RBXScriptSignal:Wait() as an alternative to :Connect so we don't have to worry about cleanup later
end
-- now, the gem is dead, so here would be where you'd destroy the gem and give rewards
task.wait(60) -- will yield the thread for 60 seconds, remember we're using a separate thread so this script can handle as many asynchronous loops as we need
end
end)() -- remember the extra pair of parentheses because coroutine.wrap creates a function to be called asynchronously, so we need to call it which the extra parentheses does.
end
Check the documentation on coroutines/multithreading here:
Also check task.delay and task.spawn as alternative methods:
Nope, just a 60 seconds cooldown for the nod to respawn as a random nod. I have like intvalues that tell me how many nods of each rarity there is, so all I need is a way to handle the cooldown of each nod that gets destroyed. For example, I destroy nod A, nod A starts its 60 seconds cooldown before respawning. During those 60 seconds, another player destroys nod B, then it starts nod B cooldown. I mean like, each nod has a 60 seconds cooldown that works “like separately” of the other nods.
Ah, then your Bindable Event will work fine for this task. Each Fire of a Bindable Event is placed on a separate thread, so multiple nodes can be regenerating at the same time without affecting one another.
When a node is destroyed, Fire your event with a clone of the destroyed node. You can then wait the time in the event’s function and then parent the cloned node to the workspace.
Oh, yea thank you, I was wondering if each time I run my Bindable event, it will make a separate thread or like “reset the cooldown” because another part has been destroyed