How do I disable tween and function after it being used

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want the UI when its opened for the 2nd time the Tween should be disabled/disconnected and shouldn’t work again

  1. What is the issue? Include screenshots / videos if possible!

When I try to fix the script it takes long for it to open or it needs multiple clicks to open.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I cannot find any solutions

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is my script:

local openingframe = game.Players.LocalPlayer.PlayerGui:WaitForChild("MilesSystem").ImageLabel.Main.Opening
local logo = openingframe:WaitForChild("Logo")

local TweenService = game:GetService("TweenService")

local function FadeInUI(ui)
	game.Players.LocalPlayer.PlayerGui.MilesSystem.Enabled = true

	wait(1)

	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local properties = {
		BackgroundTransparency = 1,
		ImageTransparency = 1,
	}

	local tween = TweenService:Create(ui, tweenInfo, properties)
	tween:Play()
end

local function FadeIn(ui)
	game.Players.LocalPlayer.PlayerGui.MilesSystem.Enabled = true

	wait(1)

	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local properties = {
		BackgroundTransparency = 1,
		ImageTransparency =0,
	}

	local tween = TweenService:Create(ui, tweenInfo, properties)
	tween:Play()
end

local function deleteUI(ui)
	wait(3)
	ui:Destroy()	
end


local button = script.Parent 

local clicked = false 

local function onOpen()
	FadeIn(logo)
	wait(3)
	FadeInUI(logo)
	FadeInUI(openingframe)
	deleteUI(openingframe)
end

local function onClose()
script.Parent.Parent.Parent.Parent.MilesSystem.Enabled = false
end

button.MouseButton1Click:Connect(function()
	if clicked == false then 
		onOpen() 
		clicked = true 
	elseif clicked == true then
		onClose() 
		clicked = false 
	end
end)
4 Likes

Add a variable to check if the player had already opened the UI.

local hasOpened = false
-- ...
local function onOpen()
if not hasOpened then
   hasOpened = true
   -- ...
end

This should stop the tween and the function.
Hope it helps!

2 Likes

I tried adding that although after closing it and trying to open it again it did not work I cannot open it…

2 Likes
local hasOpened = false

local button = script.Parent 

local clicked = false 

local function onOpen()
	
	if not hasOpened then
		hasOpened = true
		FadeIn(logo)
		wait(3)
		FadeInUI(logo)
		FadeInUI(openingframe)
		deleteUI(openingframe)
	end

end

local function onClose()
script.Parent.Parent.Parent.Parent.MilesSystem.Enabled = false
end

button.MouseButton1Click:Connect(function()
	if clicked == false then 
		onOpen() 
		clicked = true 
	elseif clicked == true then
		onClose() 
		clicked = false 
	end
end)
3 Likes

Oh, well then,
this should work

local function onOpen()
   if not hasOpened then
      hasOpened = true
   else
     -- manually change the ui's properties or create a function for it
   end
end
2 Likes

Can you give me an example I don’t understand.

2 Likes
local function onOpen()
   if not hasOpened then
      hasOpened = true
   else
     -- manually change the ui's properties or create a function for it

local function onOpen2()
script.parent.parent.parent.parent.MilesSystem.Enabled = true

?

   end
end
2 Likes

What I meant on the comment is either,
you change the properties

-- ...
else
   openingframe.BackgroundTransparency = 0
   logo.ImageTransparency = 0
end

or,
create a function to make it easier to write

local function uiOpen(ui)
    ui.BackgroundTransparency = 0
    if ui:IsA("ImageLabel") then
       ui.ImageTransparency = 0
    end
end
--...
local function onOpen()
    if not hasOpened then
       --...
    else
       uiOpen(openingframe)
       uiOpen(logo)
    end
end
3 Likes

The essence of the tween would disappear right?

1 Like

Yes, as the tween would not play anymore since you’re not calling tween:Play().

2 Likes

If I remove that it would ruin the Opening
Watch this video
https://gyazo.com/fb4990c31fdacb6b70974ba79c8a00df

2 Likes

Is there another way that I can do this?

2 Likes

Could I do If the ScreenGUI is enabled, Via the MouseButton1Click, I will enable the Tween Animation

2 Likes

Do you want to reset the animation if the user enables the UI?

2 Likes

If that fixs the problem, surely yes!

2 Likes

You can reset the tween by :Cancel()

-- once the player enables the ui
tween:Cancel() -- resets the tween back to the start
...

If that’s what you’re looking for…

2 Likes

Oh It’s not, I guess I just have to remove the tween…

2 Likes

I really want the tween without it bugging…

1 Like