How to skip / cancel a function?

I’m creating a skip button for my intro sequence.

Player:GetAttributeChangedSignal("playbuttonclicked"):Connect(function()
	if Player:GetAttribute("playbuttonclicked") == true then	
		typewriterthingy()
		skipbutton.MouseButton1Click:Connect(function()
			
		end)
	end
end)

How do I stop “typewriterthingy” function from running once skipbutton is pressed?

Try to add a Disconnect:() function.

Can I see your typewriterthingy function?

local textLabel = script.Parent.mainText
local screengui = script.Parent

screengui.Enabled = false

local typesound = Instance.new("Sound")
typesound.SoundId = "rbxassetid://1394480679"
typesound.Looped = false
typesound.Parent = game.SoundService

local function introductionDialogueMain(object,text)
	screengui.Enabled = true
	for i = 1,#text,1 do
		object.Text = string.sub(text,0.64,i)
		typesound:Play()
		wait(0.1)
	end
end

local function typewriterthingy()
	wait(3)
	introductionDialogueMain(textLabel,"Welcome to Try To Die!",0.64)
	wait(3)
	introductionDialogueMain(textLabel,"The goal of this game is to die... but with a twist...",0.64)
	wait(2)
	introductionDialogueMain(textLabel,"have fun!!!",0.64)
	wait(3)
	screengui.Enabled = false
end

local skipbutton = screengui.skipbutton
Player = game.Players.LocalPlayer

connection = Player:GetAttributeChangedSignal("playbuttonclicked"):Connect(function()
	skipbutton.MouseButton1Up:Connect(function()
		return
	end)
	if Player:GetAttribute("playbuttonclicked") == true then	
		typewriterthingy()
	end
end)

This is the whole script

An easy way to cancel it would be running the function in a separate coroutine and cancelling this coroutine.

Player:GetAttributeChangedSignal("playbuttonclicked"):Connect(function()
	if Player:GetAttribute("playbuttonclicked") == true then	
		local coroutineThread = task.spawn(typewriterthingy)
		skipbutton.MouseButton1Click:Once(function()
			task.cancel(coroutineThread)
		end)
	end
end)
3 Likes

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