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
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