Tween not Repeating every Time i Press a Key

  1. What do you want to achieve? I want the GUI to tween every time i press “T”.

  2. What is the issue? It only tweens the first time i press “T”. If i try to press it again, its pops up.

  3. What solutions have you tried so far? I’ve tried changing positions, tween styles, the entire code, but it doesn’t help.

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!

local info = TweenInfo.new(0.25)
local UserInputService = game:GetService("UserInputService")
local tween = game:GetService("TweenService")

function onKeyPress(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.T then
			if script.Parent.Visible ==  false then
				
			
				script.Parent.Visible =  true 	
				tween:Play(script.Parent:TweenPosition(UDim2.new(0.5, 0,0.970, 0), "Out", "Quint", 0.25))

	else
				script.Parent.Visible = false
				tween:Cancel()
			end
		end
	end
end
UserInputService.InputBegan:Connect(onKeyPress)
1 Like

Does it have any errors? Any errors in the output? Did it print anything?

2 Likes

You haven’t given us that much context, so I’m not sure how much help you really need, but here’s something I made for you. You can copy this code and paste it into your script. You can edit it however you’d like to.

local Item = script.Parent -- This is to make "script.Parent" not have to be used every single time you want the parent of the script.
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

function TweenGui(UI)
	local Time = 0.25 -- Change this to whatever you want.
	local EasingStyle = Enum.EasingStyle.Quint -- Change this to whatever you want.
	local EasingDirection = Enum.EasingDirection.Out
	local RepeatTimes = 0 -- You can change this to whatever you want. -1 will make it repeat itself infinitely.
	local Reversable = false -- Making it true will make it return to its original state.
	
	local Info = TweenInfo.new(Time, EasingStyle, EasingDirection, RepeatTimes, Reversable) -- The info that the tween needs.
	local Goal = {}
	Goal.Size = UDim2.new(0.5, 0, 0.970, 0) -- The goal that the tween will attempt to reach.
	local Tween = TweenService:Create(UI, Info, Goal)
end

function onKeyPress(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.T then
			if Item.Visible ==  false then
				Item.Visible = true
				TweenGui(script.Parent)
			else
				Item.Visible = false
			end
		end
	end
end

UserInputService.InputBegan:Connect(onKeyPress)

There are some issues with your script.

First of all, you’re not creating a tween. This can be done like so:

local tweenInfo = TweenInfo.new(0.25) -- 0.25 is the duration, you can add an easing style after the duration
local TweenService = game:GetService("TweenService") -- the service that handles tweens
local targetPos = UDim2.new(0.5, 0,0.970, 0) -- your target position
local tween = TweenService:Create(script.Parent, tweenInfo, {Position = targetPos}) -- now we create a tween
tween:Play() -- this plays the tween

The cancel method only works if the tween is already playing, that means if the tween has already finished playing, it will not go back to it’s starting position. That results of the tween not working anymore.
To fix that, save the starting position before you play any tween and set the position of script.Parent to the starting position after the visible was set to false.
In conclusion, we should come up with this code:

-- Services
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

-- Tween variables
local startingPos: UDim2 = script.Parent.Position
local targetPos = UDim2.new(0.5, 0, 0.970, 0)
local tweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local tween = TweenService:Create(script.Parent, tweenInfo, {Position = targetPos})

function onKeyPress(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.T then
            script.Parent.Position = startingPos -- set the position to starting position before any tween happens
            script.Parent.Visible = not script.Parent.Visible -- Set the visible to the opposite of what it is
			if script.Parent.Visible then -- if the it's visible
				tween:Play() -- then play the tween
	        elseif tween.PlaybackState ~= Enum.PlaybackState.Completed then -- if it's not visible and the tween hasn't completed
                tween:Cancel() -- then cancel the tween
			end
		end
	end
end

UserInputService.InputBegan:Connect(onKeyPress)

Hope you’ve understood my explanation!

1 Like

First, :Play() is not a method of TweenService.
Second, :Play() must not have a parameter of a tween.
Third, :TweenPosition() automatically does the tween.
Fourth (might be biased), ContextActionService is better than UserInputService here.

local CAS = game:GetService("ContextActionService")
local TS = game:GetService("TweenService")
local tween = TS:Create(script.Parent, TweenInfo.new(0.25, 5), {Position = UDim2.new(0.5, 0,0.970, 0)})

function Tween()
   script.Parent.Visible = not script.Parent.Visible
   if script.Parent.Visible then
      tween:Play()
   else
      tween:Cancel()
   end
end

CAS:BindAction("Tween", Tween, false, Enum.KeyCode.T)
1 Like

This is nice work. When using inputs with keys a lot of the time it is better to use on release not push.

Sorry for not being specific before. I was just so frustrated. The GUI was suppose to disappear after 10 seconds of it being on screen. And your supposed to be able to press it again to see the info again. The code worked but the tween didn’t after the fist time i pressed T. After that it just popped up instead of tweening up.

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