How do I make this tween stop whenever I stop holding "E"

Hey Developers!
Basically all I want to do is make an “E to Interact” gui. I want it to whenever you hold for 5ish seconds, it do something. I’m not actually sure how I’ll manage that, but I’ll get there. My current problem is the fact that I cannot for the life of me get this animation to stop. I feel a bit dumb here, but heres my current code, inside a LocalScript in StarterGui. Thanks!

Code:

local UIS = game:GetService("UserInputService")
local HRP = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')

local key = Enum.KeyCode.E
local db = false

local function isEbeingHeld()
	return UIS:IsKeyDown(key)
end
local function began()
	local TI = TweenInfo.new(
			5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.InOut,
			0,
			false,
			0
		)
		local TS = game:GetService("TweenService")
		local createAnim = TS:Create(workspace.ClassicSword.Handle.BillboardGui.eMain.Frame, TI, {Size = UDim2.new(0, 50, 0, -50)})
	if not isEbeingHeld() then
		createAnim:Stop()
	else
		createAnim:Play()
	end
end
UIS.InputBegan:Connect(function(input, isTyping)
	if input.KeyCode == key and not isTyping then
		began()
	end
end)

-- Side Code

while wait() do
	if (workspace.ClassicSword.Handle.Position - HRP.Position).magnitude < 20 then
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = math.huge
		workspace.ClassicSword.Handle.BillboardGui.Enabled = true
	else
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = 0
		workspace.ClassicSword.Handle.BillboardGui.Enabled = false
	end
end
4 Likes

(I’ve got the E to Interact working, but I cannot get the frame to stop tweening upward. I’ve tried using :Stop() and :Pause(), but they do not seem to work the way in which I’m trying to get them to…)

Consider setting up your code like this

local Tween = TweenService:Create(Object, TweenInformation, {Goal})

local function OnInputBegan(Input)
    if Input.KeyCode == KeyCode then
        Tween:Play()
    end
end

local function OnInputEnded(Input)
    if Input.KeyCode == KeyCode then
        Tween:Cancel()
    end
end

UserInputService.InputBegan:Connect(OnInputBegan)
UserInputService.InputEnded:Connect(OnInputEnded)

To fit your code

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(5,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local createAnim = TS:Create(workspace.ClassicSword.Handle.BillboardGui.eMain.Frame, TI, {Size = UDim2.new(0, 50, 0, -50)})

local function OnInputBegan(Input)
    if Input.KeyCode == KeyCode then
        createAnim:Play()
    end
end

local function OnInputEnded(Input)
    if Input.KeyCode == KeyCode then
        createAnim:Cancel()
    end
end

UIS.InputBegan:Connect(OnInputBegan)
UIS.InputEnded:Connect(OnInputEnded)
4 Likes

Sorry for the late-ish reply, I’m looking more to have it where they Hold E for 5 or so seconds, and if they release in the middle of those 5 seconds, the tween stops.

Hmm, you could set the time for the gui to move is 5 seconds, then in this function, change cancel to pause, the tween will resume when you press e again.

local function OnInputEnded(Input)
    if Input.KeyCode == KeyCode then
        createAnim:Pause()
    end
end
2 Likes

So 2 questions:

  1. Is it possible that you want the tween to reverse when they stop holding E?
  2. What is your current behavior, and how is it deviating from your intended behavior?
1 Like
  1. No, I want it to immediately cancel the tween.
  2. My current behaviour simply doesnt stop the tween whenever I let go of the E key, which I would like it to stop the tween whenever I do.

Does this still occur with the code i’ve suggested?

1 Like

Actually, theres an error I didnt mention. It said “Stop is not a valid member of tween”. Not quite sure why either, haha.

1 Like

It’s actually Cancel not Stop :disappointed_relieved: my bad!

2 Likes

I’ll try that, and everyone makes mistakes. :slight_smile:

2 Likes

Oof, so the problem isn’t… horrible?.. Though it’ll definitely break my game haha. Basically with the code I’ve posted just below, it stops the animation, yes, but I want it to set the size to 0,0,0,0 aswell. The game-breaking part would be the fact that if I continuously spam “E” it does go up, but it’s indeed very glitchy and cuts out.

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(5,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local createAnim = TS:Create(workspace.ClassicSword.Handle.BillboardGui.eMain.Frame, TI, {Size = UDim2.new(0, 50, 0, -50)})
local HRP = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')

local function OnInputBegan(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        createAnim:Play()
    end
end

local function OnInputEnded(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        createAnim:Cancel()
    end
end

UIS.InputBegan:Connect(OnInputBegan)
UIS.InputEnded:Connect(OnInputEnded)
-- Side Code

while wait() do
	if (workspace.ClassicSword.Handle.Position - HRP.Position).magnitude < 20 then
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = math.huge
		workspace.ClassicSword.Handle.BillboardGui.Enabled = true
	else
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = 0
		workspace.ClassicSword.Handle.BillboardGui.Enabled = false
	end
end

Meant to reply to you*

Have you considered this? It might solve the glitching.

1 Like

I’ve tried it, and it does indeed work, as well as I’ve made some changes to what I liked/didnt like. This it definitely a bit off topic, but any way how I’d detect when the tween is done, so I can then award the player a cloned version of the tool? (The sword in workspace is well, a tool xD)

My Current Code:

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(5,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local createAnim = TS:Create(workspace.ClassicSword.Handle.BillboardGui.eMain.Frame, TI, {Size = UDim2.new(0, 50, 0, -50)})
local HRP = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')

local function OnInputBegan(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        createAnim:Play()
    end
end

local function OnInputEnded(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        createAnim:Pause()
		createAnim:Cancel()
		workspace.ClassicSword.Handle.BillboardGui.eMain.Frame.Size = UDim2.new(0, 50, 0, 0)
    end
end

UIS.InputBegan:Connect(OnInputBegan)
UIS.InputEnded:Connect(OnInputEnded)
-- Side Code

while wait() do
	if (workspace.ClassicSword.Handle.Position - HRP.Position).magnitude < 20 then
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = math.huge
		workspace.ClassicSword.Handle.BillboardGui.Enabled = true
	else
		workspace.ClassicSword.Handle.BillboardGui.MaxDistance = 0
		workspace.ClassicSword.Handle.BillboardGui.Enabled = false
	end
end
1 Like
createAnim.Completed:Connect(function(State)
    if State == Enum.PlaybackState.Completed then
        --award
    end
end)
1 Like

This seems to work, except for the fact that if you let go, then start holding E again, the animation starts from the place where it left off.

Yeah, I forgot that the tween needed to reset, disreagrd what I’ve said, it’ll work regardless. There’s a check to see if the tween was completed or cancelled, so there should be no issues.

1 Like

I’ve made it instead of :Pause() it’s only :Cancel() Is that efficient?

1 Like

You could keep both since you said that it solves the glitchy effect, but the cancel should be sufficient.

1 Like

The cancel now completely solves both. You’ve helped me so much on the devforum, I’m so beyond happy to be on this forum with this incredible community. Thank you so much! :))

4 Likes