so im making a gui which you can open by pressing a keycode and i was wondering if there was a way to both open then close the gui with the keycode.
Yes, there is.
local userinputService = game:GetService("UserInputService")
local Frame = script.Parent.Frame -- Location of your Frame/Button..
userinputService.InputBegan:Connect(function(Key,Processed)
if Processed then return end
if Key.KeyCode == Enum.KeyCode.R then -- Change 'R' to your KeyCode.
Frame.Visible = not Frame.Visible
end
end)
Hey, this works but iām trying to make it tween back how do i do this?
For example Iām making it so when it opens it tweens open and when it closes i want it to tween to close anyknow how to do this?
i know how to tween it its just i need to know when to tween it
local UIS = game:GetService(āUserInputServiceā)
local gui = game:GetService(āStarterGuiā)
local player = game.Players.LocalPlayer
local open = script.Parent.Parent.Value
open = true
local Frame = script.ParentUIS.InputBegan:Connect(function (Input,Processed)
if Processed then return end
if Input.KeyCode == Enum.KeyCode.G then
script.Parent:TweenPosition(UDim2.new(0.123, 0,0.228, 0,āOutā,āSineā,3))
Frame.Visible = not Frame.Visible
end
end)
you could probably make it so it checks to see if itās already open and if itās already open it could tween it back
thats what im saying how would i do this
A trick I like to use is setting an attribute I name āOpenā instead of using the visible property. This allows you to use GetAttributeChangedSignal to then tween the position when it changes. The benefit is that you can interrupt the tween without worrying about it becoming invisible partway through.
sorry, im new to this whole roblox scripting thing could you maybe empathize this some more
wait, just searched up what attributes were on roblox dev i need to use a bool value right
yes a bool is what you will need to use
okay how would i use the bool value
Yes;
Essentially a menu is either open, or closed, therefore a boolean is the best way to toggle this state. However, if we just use visible and a tween, then you need to set visibility first or last depending on if its being opened, or closed, and thats annoying.
So instead, donāt use visibility and just tween them off the screen. To do this and keep its ācurrent stateā, we can use a boolean attribute and toggle that value. When this value changes, we can then tween the frame to its respected position.
Here is something I wrote up real quick:
local TS = game:GetService("TweenService")
local tween = TweenInfo.new(.2, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
local frame = script.Parent:WaitForChild("Frame")
local open_position = UDim2.new(.5,0,.5,0)
local closed_position = UDim2.new(.5,0,1.5,0)
local function updatePosition()
local is_open = frame:GetAttribute("Open") == true -- makes nil false
TS:Create(frame, tween, {
Position = is_open and open_position or closed_position
}):Play()
end
script.Parent:WaitForChild("Frame"):GetAttributeChangedSignal("Open"):Connect(updatePosition)
updatePosition()
script.Parent:WaitForChild("TextButton").MouseButton1Down:Connect(function()
local is_open = frame:GetAttribute("Open") == true -- again, nil would be turned to false here
frame:SetAttribute("Open", not is_open) -- if its open, we want it closed, if its closed, we want it open
end)
This is the result:
You can click it multiple times and it will interrupt itself without any issues.
looks good but i think i did it better
not saying you did anything wrong
Its a proof of concept, you can change the tween how you like.
thank you for helping me im really stupid