Event not disconnecting?

I’m trying to make a skip cutscene event, in which there’s a main event which fires and plays the cutscene. Then there is another clicked event that fires and is suppose to disconnect the main event using :Disconnect(). The problem is that it doesn’t disconnect, and continues playing the main event.

--Main event
mainFunction = script.Parent.Starting.OnClientEvent:Connect(function()
	contentProvider:PreloadAsync(workspace.Pit:GetChildren())
	wait(1)
	kidFolder.Ambient:Play()
	gui.Frame.TextLabel.Text = "WARNING: This game contains disturbing images, flashing lights, sudden loud noises, jumpscares, play at your own risk! (we are not responsible for any medical conditions developed)"
	toggleUI(false)
	wait(3)
	changingTextT(false)
	wait(2)
	gui.Frame.TextLabel.Text = "T H E N"
	changingTextT(true)
	wait(2)
	changingTextT(false)
	wait(1)
	skipCutscene.Enabled = true
	
	--Kid cutscene
	
	--Scene 1
	camera.CameraSubject = kidFolder.Important.Camera1.CameraPart
	camera.CameraType = "Scriptable"
	steppedCamera(kidFolder.Important.Camera1.CameraPart)
	local any = kidFolder.Important.Camera1.AnimationController:LoadAnimation(kidFolder.Important.Camera1.Moving)
	any:Play()
	any:AdjustSpeed(0.7)
	changingGUI(false)
	kidFolder.Important.Aaron.Head.Dialogue:Play()
	repeat task.wait() until not kidFolder.Important.Aaron.Head.Dialogue.IsPlaying
	changingGUI(true)
	steppedFunction:Disconnect()
	task.wait(1)
	
	--Scene 2
	changingCamera(kidFolder.Important.Camera2)
	task.wait(1)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.9
	task.wait(3)
	changingGUI(true)
	
	--Scene 3
	task.wait(1)
	changingCamera(kidFolder.Important.Camera3)
	task.wait(0.5)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.8
	task.wait(3)
	changingGUI(true)
	
	--Scene 4
	kidFolder.Important.Sparks:Play()
	kidFolder.Important.HeartBeat:Play()
	task.wait(1.5)
	changingCamera(kidFolder.Important.Camera4)
	task.wait(0.5)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.7
	task.wait(3)
	changingGUI(true)
	task.wait(1)
	
	--Scene 5 6 and 7
	changingCamera(kidFolder.Important.Camera5)
	wait(0.5)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.6
	kidFolder.Important.EvilLaugh:Play()
	wait(1)
	changingGUI(true)
	changingCamera(kidFolder.Important.Camera6)
	wait(1)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.5
	wait(1)
	changingGUI(true)
	changingCamera(kidFolder.Important.Camera7)
	wait(1)
	changingGUI(false)
	kidFolder.Ambient.PlaybackSpeed = 0.4
	wait(1)
	changingGUI(true)
	kidFolder.Ambient:Stop()
	kidFolder.Important.Scream:Play()
	repeat wait() until not kidFolder.Important.Scream.IsPlaying
	changingCamera(kidFolder.Important.Camera8)
	wait(1)
	frame.BackgroundTransparency = 1
	kidFolder.Important.HeartBeat:Stop()
	kidFolder.Important.Jumpscare:Play()
	for i = 1, 10 do
		camera.FieldOfView = camera.FieldOfView - 1
		wait()
	end
	frame.BackgroundTransparency = 0
	gui.Frame.TextLabel.Text = "N O W"
	wait(0.1)
	changingTextT(true)
	wait(2)
	changingTextT(false)
	wait(1)
	local sky = storage.CorruptedSky
	sky.Parent = game.Lighting
	game.Lighting.Ambient = Color3.new(0, 0, 0)
	game.Lighting.OutdoorAmbient = Color3.new(0.301961, 0.301961, 0.301961)
	game.Lighting.Brightness = 0
	wait(1)
	
	workspace.IntroCutscene.Ambient:Play()
	--Introduction Cutscene
	camera.CameraSubject = folder.Important.RickeyRobots.CameraPoint
	local robot = folder.Important.RickeyRobots
	local any2 = robot.AnimationController:LoadAnimation(robot.Dragging)
	
	any2:Play()
	any2:AdjustSpeed(0.5)
	steppedCamera(folder.Important.RickeyRobots.CameraPoint)
	wait(0.2)
	changingGUI(false)
	wait(1.5)
	changingGUI(true)
	wait(1)
	changingGUI(false)
	wait(1)
	changingGUI(true)
	wait(0.5)
	changingGUI(false)
	wait(0.2)
	changingGUI(true)
	wait(0.5)
	changingGUI(false)
	wait(0.7)
	changingGUI(true)
	steppedFunction:Disconnect()
	wait(2)
	local robots2 = storage.IntroCutscene.RickeyRobots2:Clone()
	robots2.Parent = workspace.IntroCutscene.Important
	camera.CameraSubject = robots2.CameraPoint
	camera.CFrame = robots2.CameraPoint.CFrame
	robot:Destroy()
	local rickey = workspace.IntroCutscene.Important.RickeyRat
	local idle = rickey.AnimationController:LoadAnimation(rickey.Idle)
	rickey.MainHead.Dialogue:Play()
	wait(1)
	changingGUI(false)
	wait(0.2)
	idle:Play()
	idle:AdjustSpeed(0.5)
	wait(1)
	changingGUI(true)
	wait(2)
	changingGUI(false)
	wait(8)
	changingGUI(true)
	gui.Frame.TextLabel.Text = "SooGloez Presents"
	changingTextT(true)
	wait(3)
	changingTextT(false)
	changingGUI(false)
	gui.Frame.TextLabel.TextTransparency = 1
	repeat wait() until not rickey.MainHead.Dialogue.IsPlaying
	changingGUI(true)
	camera.CameraSubject = character.Humanoid
	camera.CameraType = "Custom"
	wait(0.5)
	
	endCutscene()
end)



--Skipping the cutscene
skipCutscene.SkipEvent.OnClientEvent:Connect(function()
	if steppedFunction then
		steppedFunction:Disconnect()
	end
	mainFunction:Disconnect()
	changingGUI(true)
	camera.CameraSubject = character.Humanoid
	camera.CameraType = "Custom"
	local sky = storage:FindFirstChild("CorruptedSky")
	if sky then
		sky.Parent = game.Lighting
	end
	game.Lighting.Ambient = Color3.new(0, 0, 0)
	game.Lighting.OutdoorAmbient = Color3.new(0.301961, 0.301961, 0.301961)
	game.Lighting.Brightness = 0
	
	--Stopping audios
	for _, audio in pairs(kidFolder.Important:GetDescendants()) do
		if audio:IsA("Sound") and audio.IsPlaying then
			audio:Stop()
		end
	end
	endCutscene()
end)

There’s a difference between preventing the main event from being run a second time and stopping the main event altogether. Disconnect only prevents the event from triggering the function again. Or am I misunderstanding you?

1 Like

Hmmm, well then is there a way to fully stop the main event from continuing code?

You’ll have to restructure your code to support that kind of behaviour. Your current cutscene is a chain of waits and subsequent executions; you’d need to split it into callable sequences so that when a skip is triggered you can stop going through those sequences, effectively ending the cutscene.

3 Likes

What would be a good way to do this?

A somewhat messy-but-easy way you could do this is check for a cancelled flag before or after each wait:

-- ... animation ...
wait(5)
if cancelled then return end
-- ... more animation ...

This won’t work if your cancel condition can change rapidly (less than your wait time), but for one-shot-wonders it might be good enough.

Animating an interface like this is a good opportunity to use Promises, which is an object that represents one unit of asynchronous work (such as a tween, wait, etc). They feature cancelling, which is something you might be highly interested in.

2 Likes