So I have this system that locks the mouse to the center of the screen and gives the player’s camera an offset to make a third person camera system. A issue I’ve ran into was that when the player unlocks their mouse they can’t use RMB to move their camera around in like a standard game without a feature like this.
I assume the culprit of the code is this block here, just unsure how to fix it.
RS.RenderStepped:Connect(function()
if shouldUnlockMouse() then
UIS.MouseBehavior = Enum.MouseBehavior.Default
elseif mouseLocked then
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
else
UIS.MouseBehavior = Enum.MouseBehavior.Default
end
workspace.CurrentCamera.CFrame *= CFrame.new(2, 0, 0)
end)
give full context of what the script is doing, here is the rest of it.
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Players = game:GetService("Players")
local notifSystem = require(game.ReplicatedStorage.NotificationHandler)
local player = Players.LocalPlayer
local mouseLocked = true
local blockG = false
-- Cache GUI references
local mainMenuFrame
local inventoryScreen
local shopGUI
-- Function to find and cache GUI elements
local function cacheGUI()
local playerGui = player:FindFirstChild("PlayerGui")
if playerGui then
local mainMenu = playerGui:FindFirstChild("MainMenu")
if mainMenu and mainMenu:IsA("ScreenGui") then
mainMenuFrame = mainMenu:FindFirstChild("Frame")
end
inventoryScreen = playerGui:FindFirstChild("InventoryScreen")
shopGUI = playerGui:FindFirstChild("ShopGUI")
end
end
-- Check if any menu is visible that should unlock mouse
local function shouldUnlockMouse()
if mainMenuFrame and mainMenuFrame:IsA("Frame") and mainMenuFrame.Visible then
return true
end
-- Check InventoryScreen enabled state
if inventoryScreen and inventoryScreen:IsA("ScreenGui") and inventoryScreen.Enabled then
return true
end
if shopGUI and shopGUI:IsA("ScreenGui") and shopGUI.Enabled then
return true
end
return false
end
-- Set up GUI listeners
local function setupGUIListeners()
cacheGUI()
if mainMenuFrame and mainMenuFrame:IsA("Frame") then
mainMenuFrame:GetPropertyChangedSignal("Visible"):Connect(function()
mouseLocked = not shouldUnlockMouse()
end)
end
if inventoryScreen and inventoryScreen:IsA("ScreenGui") then
inventoryScreen:GetPropertyChangedSignal("Enabled"):Connect(function()
if not blockG then
mouseLocked = not shouldUnlockMouse()
end
end)
end
if shopGUI and shopGUI:IsA("ScreenGui") then
shopGUI:GetPropertyChangedSignal("Enabled"):Connect(function()
if not blockG then
mouseLocked = not shouldUnlockMouse()
end
end)
end
end
-- Initialize GUI tracking
if player:FindFirstChild("PlayerGui") then
setupGUIListeners()
else
player.ChildAdded:Connect(function(child)
if child:IsA("PlayerGui") then
setupGUIListeners()
end
end)
end
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
local menuOpen = shouldUnlockMouse()
if input.KeyCode == Enum.KeyCode.L and UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
mouseLocked = not mouseLocked
notifSystem:CreateNotification("Info", "You've unlocked your mouse by pressing Control + L, press it again to lock it back.", 5, nil)
return
end
if input.KeyCode == Enum.KeyCode.G then
if not menuOpen then
mouseLocked = not mouseLocked
end
return
end
end)
-- Apply mouse lock state
RS.RenderStepped:Connect(function()
if shouldUnlockMouse() then
UIS.MouseBehavior = Enum.MouseBehavior.Default
elseif mouseLocked then
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
else
UIS.MouseBehavior = Enum.MouseBehavior.Default
end
workspace.CurrentCamera.CFrame *= CFrame.new(2, 0, 0)
end)