I have a function that’ll countdown until something explodes, but I want it to go back to the top of the function when its parent changes at any given moment. How would I make the function do that?
script.Parent.Parent.AncestryChanged:Connect(function()
wait(4.5)
--On
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.7)
--Off
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.7)
--On
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.7)
--Off
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.4)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.4)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.25)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.25)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.25)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.25)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
local Explosion = Instance.new("Explosion", game.Workspace)
Explosion.BlastRadius = 1
Explosion.Position = script.Parent.Position
end)
DasKairo
(Cairo)
December 26, 2022, 11:21pm
#2
You would Call the Function Again:
function Something()
task.wait(1)
Something() -- Calls Function Again
end
Something() -- Initial Call
So if I reached (Example) Line 30, then the object was passed to another player, would this make it start from the begining again?
DasKairo
(Cairo)
December 26, 2022, 11:23pm
#4
I think this would just Call the function and continue, try adding return
return Something()
Would I add return to the end of the function? Never used returns before
DasKairo
(Cairo)
December 26, 2022, 11:24pm
#6
return
just returns data and ends the function early, but in this case, it ends the function early and calls itself to repeat itself
CircDev
(Circuits_Games)
December 26, 2022, 11:25pm
#7
You could use a seperate script to disable and re-enable the script
DasKairo
(Cairo)
December 26, 2022, 11:28pm
#8
That doesn’t seem like very good idea
1 Like
local function Explosion()
wait(4.5)
--On
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.7)
--Off
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.7)
--On
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.7)
--Off
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.4)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.4)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.4)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.25)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.25)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.25)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.25)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
script.Parent.TextureID = "rbxassetid://8196371175"
wait(.05)
script.Parent.TextureID = ""
script.Parent.Material = Enum.Material.Neon
script.Parent.Color = Color3.new(0.8, 0.164706, 0.164706)
wait(.05)
local Explosion = Instance.new("Explosion", game.Workspace)
Explosion.BlastRadius = 1
Explosion.Position = script.Parent.Position
return Explosion
end
script.Parent.Parent.AncestryChanged:Connect(Explosion)
Would this work?
DasKairo
(Cairo)
December 26, 2022, 11:31pm
#10
It would, but you most likely wont see it happen due to all the waits.
Would I put the return after every wait to get the effect I’m going for?
DasKairo
(Cairo)
December 26, 2022, 11:32pm
#12
return
only works once in functions as it ends the function, if you would want the code to yield, try using coroutines
What in the world are you trying to?
DasKairo
(Cairo)
December 26, 2022, 11:35pm
#14
Looks like he is trying to make a bomb
Do you mean yielding the function from running like what @DasKairo said?
I think task.spawn()
or coroutines
may help.
They basically creates a thread in the script, so the code in there will only run in that thread and loops can be placed in there without interrupting the rest of the code.
This is a simple way of creating a thread with task.spawn() and yielding it later
local thread = task.spawn(function()
print("1")
task.wait(2)
print("2")
task.wait(2)
print("3")
end)
task.wait(3)
task.cancel(thread)
I didn’t test it yet, and I’m not sure if it really cancels, but I think it does.
coroutines
is the same task.spawn()
, but with more features for complex scripting
To elaborate, this is a hot potato game. The code I put flashes the potato red and then the flashing speeds up, then it explodes and whoever is holding it blows up. When the potato gets passed to another player, I want this function to go back to the top to reset the explosion timer.
I am aware. But I need more information from OP about the topic. The replies so far, have been very ineffecient. (Not gonna judge, just putting out a fact here)
1 Like
Could I do something like this?
game.ReplicatedStorage.HotPotatoEvents.TimerReset.Event:Connect(function()--BindableEvent is from a seperate script that handles the model being swapped from player to player
task.cancel(ExplosionFunction)
wait()
task.spawn(ExplosionFunction)
end)
1 Like
DasKairo
(Cairo)
December 26, 2022, 11:47pm
#19
Just an idea
task.spawn(function()
local Time = 2
while Time >= .1 do
-- Part 1 code
task.wait(Time/2)
-- Part 2 code
task.wait(Time/2)
Time -=.1
end
-- Explode
end)
Obviously this isnt great but, Just an idea neither less.
Yes, but I have a feeling that it may error because you’re canceling what’s already canceled.
I may be wrong, but just in case, maybe add a variable and a sanity check to make sure that the thread is resuming to cancel it. Run the game and then tell us if it worked.
local threadResuming = false
game.ReplicatedStorage.HotPotatoEvents.TimerReset.Event:Connect(function()--BindableEvent is from a seperate script that handles the model being swapped from player to player
if threadResuming then
task.cancel(ExplosionFunction); threadResuming = false
end
task.wait()
task.spawn(ExplosionFunction); threadResuming = true
end)