How would I "break" function, when the function is running?

Hey Devs,
I am curious how would I break function, when function is running.
I am now trying to make if player is holding the mouse it will show ui with tween and the player needs to hold the mouse to the end of tween to recieve the reward but unfortunately I dont know how I should break the function if player stops holding the mouse.
For reference I am trying to accomplish something similiar to Bubble Gum Simulator Bubble bubble blowing.

Heres the script:

local UIS = game:GetService('UserInputService')
local deb = false
UIS.InputBegan:Connect(function(key, reserved)
	if not reservedthen 
		if key.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDown = true
            if not deb then
               deb = true
               tweenfunction()
		       print("Holding")
               wait(1)
               deb = false
            end
		end
	end
end)

UIS.InputEnded:Connect(function(key, reserved)
	if not reserved then
		if key.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDown = false
			print("Stopped")
           --possibly break the function here??
		end
	end
end)

Thanks for any possibly solution for this!

Use return, it’ll stop the function

I tried to put this into the function

if not MouseDown then return end

Just put return, you don’t need an if

UIS.InputEnded:Connect(function(key, reserved)
	if not reserved then
		if key.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDown = false
			print("Stopped")
            return -- return can be used without an if condition
            -- everything below this point in the function won't run (it'll error)
		end
	end
end)

Although, looking at the function, why would you need to stop it?

If you’ve ever played Bubble Gum Simulator you most likely know how the thing I am trying to accomplish works.
Basically you need to hold the mouse until the “tween” ends and right now I can just normal click and I will still get the reward .
Sorry if it still sounds confusing for you.

Oh, for something like this, it would be easier to use ContextActionService

local contextActionService = game:GetService("ContextActionService")
local tweenDidFinish = false

local stopTween = Instance.new("BindableEvent")
local _tweenCompleted -- this is the variable to connect to the tween's completion

local function hold(name, state, object) -- context action service returns the name, input state, and object
  if state == Enum.UserInputState.Begin then
      -- user started to hold the mouse down

      stopTween.Event:Connect(function()
          if _tweenCompleted then
             _tweenCompleted:Disconnect() -- disconnect the event
          end

          tween:Cancel() -- stop the tween
      end)

      _tweenCompleted = tween.Completed:Connect(function()
          tweenDidFinish = true
      end)
  elseif state == Enum.UserInputState.End then
      -- user let go of the mouse 

      if not tweenDidFinish then
          -- tween didn't finish
          stopTween:Fire() -- fire this event and the tween function should connect to it and cancel the tween
      else
          -- tween did finish
      end
  end

   return Enum.ContextActionResult.Pass
end

--                              action name  func  mobile         keycodes to bind
contextActionService:BindAction("HoldMouse", hold, false, Enum.UserInputType.MouseButton1)
4 Likes

I will try this out thank you :)!

Okay and how would I accomplish the function break because the tween will still go or am I wrong?

I am using TwenSize so it would be probably better to do it like @HugeCoolboy2007 said.

May I have question?
Is tween in your script function or variable?

In my script, tween is a variable that’s created from a function. If you need any changes let me know

Sorry I am kinda confused because u are not referencing the variable in your script if I am not wrong.

Oh yea, that’s because It was just to show what you’re suppose to do with the tween variable (when you define it)

So like

local tween = guiObject:TweenSize(blah blah)

?

Yea like that, but you to might have to use TweenService since GuiObject:TweenSize doesn’t allow for artificial cancellation (Tween:Cancel()) without creating another tween under the same object. But you can counter this by creating another tween under the object like I mentioned

GuiObject:TweenSize(...)
GuiObject:TweenSize(...) -- if you have the "override" parameter valued "true" then this will cancel the tween above

The code above I gave uses TweenService but you can change it to fit your needs

Alright so I will give it a try with the TweenService and let you know thanks :wink: !

Hm I saw now the documentation and I came with an idea can I just try to use the callback when the mouse is not held anymore?

1 Like

Yea, you could try that. It might work