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