So I wanna make this for my gun that when the players fires their first shot (by pressing mousebutton1) it immediately puts the player into shiftlock , ive tried looking for certain scripts , couldnt find them i found one of them but it dosent work properly
My game uses PlayerModule for an inventory system and shiftlock glitches with it
How would i go on making that when one shot is fired from the gun it puts the player into shiftlock without bugging out
(The bug i faced before was it was in shiftlock but i was not able to move the camera using my mouse)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local function enableShiftLock()
-- Enable shift lock mode
player.DevEnableMouseLock = true
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
-- Set the camera mode to support shift lock
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Custom -- or Scriptable if you want more control
end
-- Function to detect the first shot fired
local function onFirstShot()
enableShiftLock()
end
-- Example for detecting first shot using a tool's `Activated` event
local tool = script.Parent -- Assuming the script is a child of the tool
tool.Activated:Connect(function()
onFirstShot()
end)
I heavily apologize sir I did not at all test what I sent otherwise I would have seen the Output that says you can’t use that method. I have setup a workaround framework for you that should do what you intended. You can modify it as needed for your gun system. I tested with a Tool object and Handle inside tool object. Script is a LocalScript.
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local shiftLockEnabled = false
-- Enable shift lock
local function enableShiftLock()
shiftLockEnabled = true
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
Camera.CameraType = Enum.CameraType.Custom
end
-- Disable shift lock
local function disableShiftLock()
shiftLockEnabled = false
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
Camera.CameraType = Enum.CameraType.Custom
end
-- Update character orientation to match camera direction
local function updateCharacterOrientation()
if shiftLockEnabled then
-- Align the character's root part to the camera's look direction
local cameraCFrame = Camera.CFrame
local lookDirection = cameraCFrame.LookVector
-- Calculate the rotation for the character
local newCFrame = CFrame.new(humanoidRootPart.Position, humanoidRootPart.Position + Vector3.new(lookDirection.X, 0, lookDirection.Z))
-- Update character's rotation (only Y-axis)
humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, math.atan2(-lookDirection.X, -lookDirection.Z), 0)
end
end
-- Tool activation to enable shift lock
local tool = script.Parent
tool.Activated:Connect(function()
if not shiftLockEnabled then
enableShiftLock()
end
end)
-- Tool unequipped to disable shift lock
tool.Unequipped:Connect(function()
disableShiftLock()
end)
-- Update character rotation on every frame
RunService.RenderStepped:Connect(updateCharacterOrientation)