Creating a skip function for an intro cutscene

I want to create a skip button but have had some issues with stopping the rest of the script.
I’m using UserInputService inside of the larger animation function but I can’t find a way to cancel or pause the function inside of the InputBegan function.

spawn(function() -- example script
	local uisIB
	uisIB = UIS.InputBegan:Connect(function(input, gpe)
		if gpe == true then return end

		if input.KeyCode == Enum.KeyCode.Space then
			if SkippedIntro == false then
				uisIB:Disconnect()
				SkippedIntro = true

				return -- this doesnt stop the entire function though
			end
		end
	end)

	-- Animation code
end)

looks like you’re trying to use return to exit the function when the space key is pressed, but the return statement only exits the current function, not the surrounding one. To achieve the desired behavior, you should use a different approach.

you should try doing something like this:

local UIS = game:GetService("UserInputService")

local SkippedIntro = false

spawn(function()
    local uisIB
    local skipFlag = false

    uisIB = UIS.InputBegan:Connect(function(input, gpe)
        if gpe then
            return
        end

        if input.KeyCode == Enum.KeyCode.Space then
            if not SkippedIntro then
                skipFlag = true
                uisIB:Disconnect()
                SkippedIntro = true
            end
        end
    end)

    -- Animation code
--here u can do a while true do

I have tweens down there in the animation code. Would a while loop make the tween run multiple times?

yes, but you need to implement them right

I’m not sure I understand? If the tweens repeat that would break them right? I want to know if there’s another way I can return the function.

This should work:

local thread
thread = coroutine.create(function()
	local uisIB
	uisIB = UIS.InputBegan:Connect(function(input, gpe)
		if gpe == true then return end

		if input.KeyCode == Enum.KeyCode.Space then
			if SkippedIntro == false then
				uisIB:Disconnect()
				SkippedIntro = true

				coroutine.close(thread)
			end
		end
	end)

	-- Animation code
end)
coroutine.resume(thread) -- You'll need to use this to start the function
1 Like

This works ! Thank you so much !!

1 Like

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