Function keys do not work with UserInputService

In my game I have it so players are locked in first person. However, I have some on-screen elements that they need to interact with, so occasionally I’d like them to have their mouse free. I want the player to be able to free their mouse whenever they press [F3].

The issue is that this will not work with the F3 key. Here is how I have the applicable part of the script written. This is written in a LocalScript placed inside StarterPlayerScripts.

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F3 then
		ToggleFreeMouse();
	end
end)

I tried searching but was unable to find anyone having trouble using the function keys. I also tried binding it to another key, such as [F], and it worked just fine. Is it even possible to utilize the function row for keybinds inside Roblox?

Here is the entire script, if it matters.

local Players = game:GetService("Players");
local UserInputService = game:GetService("UserInputService");

local player = Players.LocalPlayer;
local PlayerHud = player.PlayerGui:WaitForChild("PlayerHud")
local Crosshair = PlayerHud.Crosshair;
local FreeMouse = PlayerHud.FreeMouse;

UserInputService.MouseIconEnabled = false; -- Default setting

Crosshair.Visible = true; -- Default setting

local function ToggleFreeMouse()
	if not FreeMouse.Visible then
		FreeMouse.Visible = true;
		UserInputService.MouseIconEnabled = true;
		Crosshair.Visible = false;
		print("Mouse toggled on!");
	else
		FreeMouse.Visible = false;
		UserInputService.MouseIconEnabled = false;
		Crosshair.Visible = true;
		print("Mouse toggled off!");
	end
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F3 then
		ToggleFreeMouse();
	end
end)

Are you sure your pressing function down and not just F3?

1 Like

You sure you’ve tested it outside studio?
I mean that is probably because of the studio’s F3 shortcut:

3 Likes

I tried it while holding FN and no luck. The function row on my keyboard is dedicated anyway (SteelSeries Apex 7), so it probably doesn’t make a difference. Thank you though.

I thought I had, but I must have not published it properly or something, because now it works. I guess I’ll use a different keybind while testing. Thank you.

Just use RunService:IsStudio() to change the keybind to something else when testing.

local KeyBind = Enum.KeyCode.F3
local RunService = game:GetService("RunService")

if RunService:IsStudio() then
    KeyBind = Enum.KeyCode.T
end
2 Likes

Thank you for this, I was about to do something a lot less efficient.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.