Quick Power Selection via Keybinds?

What I essentially want to do is allow the player to switch between different powers/abilities by pressing either the Q and E key, I’ve been able to get a somewhat ok foundation (It can currently detect which key was pressed):

https://gyazo.com/a6d448984ccb0ea899a7ef11b3512547

The only problem however is getting the GUI/HUD to display correctly:


https://gyazo.com/2f93de91b7c0c8442ec5f0b08921ccf6

It sometimes randomly pops up/flashes in some cases when in reality, I would like it to be more of this effect (Close example of what I kinda want it to be):
https://gyazo.com/7045323645b644c58e9dc1a7eb9fa8e2

Though a little different where when a player presses Q and E even when the HUD is invis, it automatically pops up then you can press it again to switch between which moves to use then pressing another key to set it.

EDIT: If its also possible, could I make it gone after the player first opens it in some amount of seconds? I’d assume Ill have to use tick() or wait() in some sort.

Was just testing though so this isn’t going to be exactly what it is, spare me :sweat::

local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")
local InputService = game:GetService("UserInputService")
local PowerSelectUI = script:WaitForChild("PowerDisplay")

-- Variables
local CloseTime = 2
local LocalPlayer = PlayerService.LocalPlayer

-- PropertySetting
PowerSelectUI.Adornee = LocalPlayer.Character:WaitForChild("HumanoidRootPart")

-- Bools
local UserIsTyping = false
local CurrentlyUsing = false
local JustPressed = false
local JustChanged = false

-- VariableStores
local QConnection
local EConnection
local DConnection

local function ChangeTorsoColor()
	if LocalPlayer.Character then
		if LocalPlayer.Character:WaitForChild("HumanoidRootPart") then
			LocalPlayer.Character:WaitForChild("HumanoidRootPart").Color = Color3.fromRGB(255, 255, 255)
			print("Good")
		end
	end
end
local function AutoClose()
	if JustPressed and CurrentlyUsing and JustChanged then
		repeat wait() until not JustPressed or not CurrentlyUsing and not JustChanged
	else
		CurrentlyUsing = false
	end
end
local function PopDisplay()
	if not PowerSelectUI.Enabled then
		PowerSelectUI.Enabled = true
	else
		PowerSelectUI.Enabled = false
	end
end


InputService.TextBoxFocused:Connect(function()
	UserIsTyping = true
end)
InputService.TextBoxFocused:Connect(function()
	UserIsTyping = false
end)
InputService.InputBegan:Connect(function(Input, Processed)
	if Processed then
		return
	end
	if not UserIsTyping and Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Enum.KeyCode.Q and not JustChanged then
		QConnection = coroutine.wrap(function()
			CurrentlyUsing = true
			JustPressed = true
			JustChanged = true
			print("PressedLeft/Q")
			warn("Has plr just pressed?")
			print(JustPressed)
			warn("Has plr just changed?")
			print(JustChanged)
			PopDisplay()
			wait(1)
			JustChanged = false
			warn("Has plr just changed?")
			print(JustChanged)
			wait(2)
			JustPressed = false
			warn("Has plr just pressed?")
			print(JustPressed)
		end)()
	elseif not UserIsTyping and Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Enum.KeyCode.E and not JustChanged then
		EConnection = coroutine.wrap(function()
			CurrentlyUsing = true
			JustPressed = true
			JustChanged = true
			print("PressedRight/E")
			warn("Has plr just pressed?")
			print(JustPressed)
			warn("Has plr just changed?")
			print(JustChanged)
			wait(1)
			JustChanged = false
			warn("Has plr just changed?")
			print(JustChanged)
			wait(2)
			JustPressed = false
			warn("Has plr just pressed?")
			print(JustPressed)
		end)()
	end
end)

How would I attempt this in the best way possible?

2 Likes

What do you mean by this?

Additionally, do you have any code you could show us?

The auto-timeout your talking about can be accomplished through various methods. I would recommend doing it similar to this:

local RunService = game:GetService("RunService");

local TIME_OUT = 2

local heartbeat, latestChange
local guiClose = function()
    —make things invisible.
    heartbeat:Disconnect()
end

local GuiOpen = function()
	latestChange = tick()
		heartbeat = RunService.Heartbeat:Connect(function()
			if tick()-latestChange >= TIME_OUT then
				guiClose()
			end
		end)
end
4 Likes

I would recommend using wait() over checking the tick() every frame as they do the exact same thing and connections produces unneeded RAM usage. After all, they both check every frame which make both of them as inaccurate as each other.

3 Likes