Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Keyboard input not causing GUI to tweensize?

So first off if my last posts didn't make it obvious, i SUCK at Tweening. So what i am trying to do is make a Keyboard input cause a GUI to change size when clicked.

local userInputService = game:GetService("UserInputService")
local GUI = game.StarterGui.Stamina.Super.Bar

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.G then
		script.Parent.Text = "State: Super"
		GUI.Size = UDim2.new((0) * 1, 0, 1, 0)
		wait(5)
		script.Parent.Text = "State: Normal"
		GUI.Size = UDim2.new((1) * 1, 0, 1, 0)
	end
end)

i would really appreciate help

1 comment

0

Please include what type of script it is and where it is located. It would be happily apreciated if you could include any eventual errors aswell.

2 answers

Here's whats wrong and how you can fix it: So what's wrong? Well, you're referencing to GUI as game.StarterGui.Stamina.Super.Bar. This is the GUI that is "given" to all players when they join, but the one given to them is a clone, not the actual GUI itself. If you tried to change the actual GUI itself, it will only apply when they reset, and even then, it's not going to work because the GUI resets on death (which is something you can turn off in properties).

So how do you fix it? Well, first off, you're not tweening it. You're simply setting it's size. What you want to do is use the TweenSize method of GUIs. So GUI:TweenSize(UDim2Size, EasingDirection, EasingStyle). Second, you want to make sure to reference to the correct GUI, the one the current player has.

https://developer.roblox.com/en-us/api-reference/function/GuiObject/TweenSize

Here is a fixed script:

local p = game.Players.LocalPlayer
local pgui = p:WaitForChild("PlayerGui")
local userInputService = game:GetService("UserInputService")
local GUI = pgui.Stamina.Super.Bar

userInputService.InputBegan:Connect(function(input, gameProc) -- gameProcessed makes sure you're not typing when you press the key
    if input.KeyCode == Enum.KeyCode.G and not gameProc then
        script.Parent.Text = "State: Super"
        GUI:TweenSize(UDim2.new(0, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 1, true); -- You tried to tween it's size to 0 on the x-axis, so it would go to 0, so just change it to 0 instead of multiplying
        wait(5)
        script.Parent.Text = "State: Normal"
        GUI:TweenSize(UDim2.new(1, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 1, true)
    end
end)

2 comments

0

You're not telling where to put the script, or what type of script it is. I understand where and what it should be but please mind beginners

0

Lol. He would simply need to replace his script with the one I sent. That would normally be the first thing he tries, and if he does, it'll work.

You basically used TweenSize wrong. How do you use it, I will explain. Here is an example for how do you use TweenSize..

local Frame = --YOUR TARGET TO TWEENSIZE--
local Button = --USE YOUR BUTTON THAT THE EVENT IS ON HIM--

Button.MouseButton1Click:Connect(function()
   Frame:TweenSize(
   UDim2.new(X,Y,Z), --Requested size
   "In" --Easing direction
   "Quad" --Style
   5 --Time to finish everything
   )

Now. Basically, this script is the right way on how to use TweenSize. With what you did, just switch the lines with it.

I hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.