My script isnt halting everything after the script if it runs "didWork()"

heres the code:

local cd = workspace.ShutdownButton.Buttons.Main_Power_Button.ClickDetector

			local timeoutTime = 60

			local function didWork() -- if the randomizer worked
				print("shutdown success")
				wait(2)
				AnnouncerBeep:Play()
				wait(1)
				workspace.Sounds.StartupFailMusic:Pause()
				workspace.AnnouncerMessages.UniversalShutdownMessages.Message1:Play()
				wait(9)
				local CoreVFXLower = TweenService:Create(workspace.Sounds.CoreVFX, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {PlaybackSpeed = 0})
				local CoreDissapear = TweenService:Create(workspace.Core, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Transparency = 1})
				local CoreShieldDissapear = TweenService:Create(workspace.CoreForceField, TweenInfo.new(10, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Transparency = 1})
				local LasersDischarge = TweenService:Create(workspace.Values.LaserLightsUpperValue, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Value = Color3.fromRGB(24, 24, 24)})
				local StabLasersDischarge = TweenService:Create(workspace.Values.StabLasersUpperValue, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Value = Color3.fromRGB(24, 24, 24)})
				CoreVFXSlowRaise:Pause()
				task.wait()
				workspace.Sounds.ShutdownMusic:Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser1, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser2, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser3, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser4, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser5, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser6, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser7, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				TweenService:Create(workspace.Values.LaserValues.CoreLaser8, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Value = 1}):Play()
				CoreVFXLower:Play()
				CoreDissapear:Play()
				CoreShieldDissapear:Play()
				wait(17)
				TweenService:Create(workspace.Shockwave, TweenInfo.new(6, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0), {Size = Vector3.new(440, 440, 440)}):Play()
				workspace.Sounds.Shockwave:Play()
				workspace.Shockwave.Transparency = 0
				wait(6)
				workspace.Shockwave.Transparency = 1
				wait(2)
				workspace.Core.CoreEffects.Fire1.Enabled = false
				workspace.Core.CoreEffects.Fire2.Enabled = false
				workspace.Core.CoreEffects.CoreFailVFX.Enabled = false
				workspace.Core.StartupEmitter.StartParticles.Enabled = false
				wait(4)
				workspace.Sounds.LaserChargeDown:Play()
				workspace.Sounds.LightsOff:Play()
				ReactorSealClose:Play()
				workspace.Values.ReactorChamberLightsColor.Value = Color3.fromRGB(24, 24, 24)
				workspace.Values.FacilityLightsColor.Value = Color3.fromRGB(24,24,24)
				workspace.Values.LaserLightsControlValue.Value = Color3.fromRGB(24,24,24)
				workspace.Values.CoreLineAuraValue.Value = false
				LasersDischarge:Play()
				StabLasersDischarge:Play()
				wait(10)
				local ESDSrandomizer = math.random(1, 1)
				if ESDSrandomizer == 1 then
					TweenService:Create(workspace.Sounds.ShutdownMusic, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {PlaybackSpeed = 0}):Play()
					wait(5)
					workspace.Sounds.ESDS_Music:Play()
					workspace.AnnouncerMessages.ESDS:Play()
					wait(8)
					workspace.Sounds.LightsOn:Play()
					workspace.Values.ReactorChamberLightsColor.Value = Color3.fromRGB(140, 0, 255)
					workspace.Values.FacilityLightsColor.Value = Color3.fromRGB(140, 0, 255)
					wait(5)
					ReactorSealOpen:Play()
				else
				wait(20)
				local copiedMap = game.ServerStorage:FindFirstChild("CopiedMap")

				if copiedMap then
					print("running")

					for i, v in ipairs(workspace:GetChildren()) do
						if v.Name == "Terrain" or v.Name == "Camera" or game.Players:GetPlayerFromCharacter(v) or v.Name == "WorkspaceCopier" or v.Name == "WorkspaceReloader" then continue end
						print("destroy")
						v:Destroy()
					end
					task.wait() -- wait for a bit before it clones everything back into workspace..
					for i, v in ipairs(copiedMap:GetChildren()) do
						v.Parent = workspace
					end
				else
					print(copiedMap)
				end
			end
				end
				

			local function didntWork() -- if it didnt work
				print("shutdown fail")
				wait(2)
				AnnouncerBeep:Play()
				wait(1)
				workspace.Sounds.StartupFailMusic:Pause()
				workspace.AnnouncerMessages.UniversalShutdownMessages.Message1:Play()
			end

			local function onPressed() -- when it is pressed within time
				print("I just got pressed")
				local randNum = Random.new():NextInteger(1, 1)
				if randNum == 1 then
					didWork()
				else
					didntWork()
				end
			end

			local canWork = true
			local cdConn = cd.MouseClick:Connect(function() -- this is the pressed connection
				if canWork then
					onPressed()
				end
			end)

			local totaltime = 0
			local conn
			conn = game:GetService("RunService").Heartbeat:Connect(function(dt)
				totaltime += dt
				if totaltime >= timeoutTime then
					-- past time
					print("The shutdown button was not pressed")
					canWork = false
					cdConn:Disconnect() -- dont allow any more presses
					conn:Disconnect() -- stop the loop
				end
			end)

my problem is, when didWork() is ran, it doesnt stop everything after the script, and thats a problem for me, i dont know how to solve this myself, so please help

A simple way would be to set a boolean to true when the function runs, and in all the other functions insert the following into the code after each wait():

if booleanName then return end

(replace booleanName with the name of the bool)

tried implementing this, and it didnt work

i dont think i implemented it properly, lemme try again

How did you find out that the script is not being yielded? Did you trigger the ClickDectector.MouseClick event a second time after you just triggered it?

it is being yielded, and i dont want it to continue to be yielded if didWork() runs

got @MegabyteOfficial’s solution to work

1 Like

OK. Maybe doing this will help: wrap the calling of the function didWork() in a task.spawn(); or wrap the whole function didWork() in a task.spawn().

Example 1:

if randNum == 1 then
    task.spawn(didWork)

else
    didntWork()
end

Example 2:

local function didWork()
    task.spawn(function()
        -- Write the code inside of the didWork() function here.
    end)
end

Edit: Oops. I just saw your second message.

nah its fine

char limit

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