Hello Robloxians and devs, if your game is first person and you have GUI which needs to be clicked with a mouse to interact with, then you’re in for a treat. Because in this tutorial, you will learn how to make unlock mouse in first person script. (Please note that this only works in games with locked first person.)
Before scripting
-
Make a ScreenGui. Optionally, name it MouseUnlock.
-
Add in a TextButton or ImageButton. Set its active property to false, so that players can’t interact with it.
-
Set AnchorPoint to {0.5, 0.5} and position to {0.5, 0.5}
-
Set size to {1,0, 1,0}
-
(If you used a text button, ignore). Set the image id to 0 and background transparency to 1 so that it’s not visible.
-
If you used a text button, set background and text transparency to 1.
-
Select the ScreenGui and set IgnoreGuiInset to true. This will prevent the button being offset to the bottom of the screen.
-
Move the ScreenGui to StarterGui
Scripting
-
Insert a LocalScript into StarterGui.
-
Insert this script:
local mouseUnlock = script.Parent.MouseUnlock.ImageButton --change if necessary
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C --you can change the keybind.
if mouseUnlock.Modal == false then
mouseUnlock.Modal = true
else
mouseUnlock.Modal = false
end
end
end)
How it works:
It will check if C is pressed. If modal is not enabled, it will enable. The Modal property makes it that the player can move their mouse freely in first person mode. If not, it’ll do vice-versa.
Congratulations for completing this tutorial!