Keybindings for gui

so i have this script which opens and closes both a teleporting and menu gui, IGNORE ALL THE OTHER STUFF PLS AND DONT CHANGE IT. what i want it to do is you can still click them to open but i also want it so if you click T it opens the teleport gui and M for the menu. this script is made by my friend and it all works so be carefull not to change all the other stuff :slight_smile:


repeat wait() until game:IsLoaded()

local GUI = script.Parent
local Frame = GUI:WaitForChild("Frame")
local TeleportMenu = GUI:WaitForChild("TeleportMenu")
local ToggleMenu = GUI:WaitForChild("ToggleMenu")
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local Remote = Remotes:WaitForChild("Teleport")
local ResetRemote = Remotes:WaitForChild("ResetData")
local SG = game:GetService("StarterGui")
local MenuShowing = false
local TeleportDebounce = tick()
local TeleportDebounceTime = 2
local TeleportShowing = false
local MusicButton = Frame:WaitForChild("Music")
local Teleport = Frame:WaitForChild("Teleport")
local Player = game:GetService("Players").LocalPlayer
local SecretRemote = Remotes:WaitForChild("SecretRemote")
local TextLayout = "%s/%s Secrets Found"
local SecretMaxCount = 13
local SecretCounter = Frame:WaitForChild("SecretCounter")
local ResetButton = Frame:WaitForChild("Reset")
local MsgBox = GUI:WaitForChild("MsgBox")
--local Music = Instance.new("Sound")
--Music.SoundId = "rbxasseti//288464336"
--Music.Volume = 0.7
--Music.Name = "Music"
--Music.Looped = true
--Music.Parent = workspace
--Music:Play()
Frame.Position = UDim2.new(0.5, 0, -0.179, 0)
GUI.Enabled = true

ResetButton.Activated:Connect(function()
	local NewMsgBox = MsgBox:Clone()
	NewMsgBox.Visible = true
	NewMsgBox.Parent = GUI
	local Confirm = NewMsgBox:FindFirstChild("Confirm")
	local YesBox = NewMsgBox:FindFirstChild("Box")
	local Close = NewMsgBox:FindFirstChild("Title"):FindFirstChild("Close")
	if not Confirm or not YesBox or not Close then return end
	Confirm.Activated:Connect(function()
		if string.lower(YesBox.Text) == "yes" then
			ResetRemote:FireServer()
			NewMsgBox:Destroy()
		end
	end)
	Close.Activated:Connect(function()
		NewMsgBox:Destroy()
	end)
end)

function UpdateCounter(Count)
	if not Count then return end
	SecretCounter.Text = string.format(TextLayout, Count, SecretMaxCount)
end

SecretRemote.OnClientInvoke = function(Action, Other)
	if Action == "UpdateCounter" and Other.UpdatedCount then
		UpdateCounter(Other.UpdatedCount)
	end
end

UpdateCounter(SecretRemote:InvokeServer("GetCounter"))

Teleport.Activated:Connect(function()
	TeleportShowing = not TeleportShowing
	if TeleportShowing then
		TeleportMenu:TweenPosition(UDim2.new(0.06, 0, 0.472, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
		else TeleportMenu:TweenPosition(UDim2.new(-0.061, 0, 0.472, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
	end
end)

ToggleMenu.Activated:Connect(function()
	MenuShowing = not MenuShowing
	if MenuShowing then
		Frame:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
		else Frame:TweenPosition(UDim2.new(0.5, 0, -0.179, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quart, 1, true)
	end
end)

MusicButton.Activated:Connect(function()
	local PS = Player:FindFirstChild("PlayerScripts")
	if not PS then return end
	local Music;
	for i, v in pairs(PS:GetDescendants()) do
		if v.Name == "BGM" then
			Music = v
			break
		end
	end
	if Music then
		if Music.Volume == 1 then
			MusicButton.Text = "Music: Off"
			Music.Volume = 0
			else MusicButton.Text = "Music: On" Music.Volume = 1
		end
	end
end)

for i, v in pairs(TeleportMenu:GetChildren()) do
	if v:IsA("TextButton") then
		v.Activated:Connect(function()
			local Character = Player.Character
			if not Character then return end
			local Humanoid = Character:FindFirstChild("Humanoid")
			if not Humanoid then return end
			if not ((tick() - TeleportDebounce) > TeleportDebounceTime) then
				return
			end
			if Humanoid.Sit then
				SG:SetCore("SendNotification", {Title = "Notification", Text = "Cannot teleport while seated.", Duration = 5, Button1 = "Ok"})
				return
			end
			Remote:FireServer(v.Name)
			TeleportDebounce = tick()
		end)
	end
end

There are a lot of ways you can do this, but the way that i normally do it is with ContextActionService

Here is an example of the BindAction function that you use to connect the function (functionToBind) with the button (inputTypes)

local CAS = game:GetService("ContextActionService")
CAS:BindAction( actionName, functionToBind, createTouchButton, inputTypes)

can you put the both teleport and menu gui in the script? idk how to script and my friend is off limits to edit the script

I think this works

Add this to the top

local CAS = game:GetService("ContextActionService")

Add this to the bottom

CAS:BindAction("OpenMenue", function(name, state)
	if state == Enum.UserInputState.Begin then
		MenuShowing = not MenuShowing
		if MenuShowing then
			Frame:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
			else Frame:TweenPosition(UDim2.new(0.5, 0, -0.179, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quart, 1, true)
		end
	end
end, false, Enum.KeyCode.M)

CAS:BindAction("OpenTeleport", function(name, state)
	if state == Enum.UserInputState.Begin then
		TeleportShowing = not TeleportShowing
		if TeleportShowing then
			TeleportMenu:TweenPosition(UDim2.new(0.06, 0, 0.472, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
			else TeleportMenu:TweenPosition(UDim2.new(-0.061, 0, 0.472, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
		end
	end
end, false, Enum.KeyCode.T)
1 Like

with add you mean just add it and nothing else? a space a enter just nothing else? right

Just copy the code and paste it at the top and the bottom of the script

thats all

1 Like

correct me if im wrong but the orange line under CAS means theres an error? or something wrong atleast
image

Did you remember to add

local CAS = game:GetService("ContextActionService")

to the top of your script?

1 Like

it all works! thanks! (30 char)

1 Like