Gui keybind doesnt work

Im trying to make a admin only gui but it doesnt seem to work.
I tried making only keybind function without ids but it didnt work too

local playerdata = {
	85304704, -- pxnd
	93749278, -- rainbow
}
local player = game:GetService("Players").LocalPlayer

player:GetMouse().KeyDown:Connect(function(id,o)
	if id == not table.find(playerdata, tostring(player.UserId)) then return end
	if o == "o" then
		script.Parent.Enabled = not script.Parent.Enabled
	end
end)
1 Like

There is a more reliable method for detecting keyboard inputs: UserInputService
Here is an example script:

local UserInputService = game:GetService("UserInputService")


UserInputService.InputBegan:Connect(function(gameProcessed,input)
	if gameProcessed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.A then
		print("a pressed!")
	end
end)

“gameProcessed” is a bool (true/false) and is true if the player is typing in chat for example.
“input” is an InputObject.

EDIT: Next time if you have a problem tell us if you’ve received any errors!

There are a couple issues with your script:

  1. Mouse.KeyDown is Deprecated
  2. As previous poster states, you can use UIS, much better for your use case here.

Here is an example script which I believe fits all your needs. Let me know if you have issues.

Also from previous usage of UIS, for some reason studio doesn’t always register when I click “O” because it’s used for zooming in and out. Not sure if that’s a bug or intended to prevent clashing of the two buttons but using “O” works in game, though. For the sake of testing I changed this to “E” for you though.

local UIS = game:GetService("UserInputService")

local playerdata = {
	85304704, -- pxnd
	93749278, -- rainbow
}


local player = game.Players.LocalPlayer

UIS.InputBegan:Connect(function(key, gameProcessed)
	if not gameProcessed then
	if not table.find(playerdata, player.UserId) then return end
	if key.KeyCode == Enum.KeyCode.E then -- change to Enum.KeyCode.O or something else if u want
		script.Parent.Enabled = not script.Parent.Enabled
		end
	end
end)

it worked now thank you so much

local UserInput = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ScreenGui = script.Parent

local Whitelist = {85304704, 93749278}

UserInput.InputBegan:Connect(function(Key, Processed)
	if Processed then return end
	if Key.KeyCode == Enum.KeyCode.E then
		if table.find(Whitelist, Player.UserId) then
			ScreenGui.Enabled = not ScreenGui.Enabled
		end
	end
end)

Would be better to move the ID check to inside the KeyCode check.

2 Likes