[OFF-SET] if i Change Humanoid.CameraOffset, it looks weird with Only-Tool mode
[AIM] Even though there is nothing to affect mouse pos, it sometimes randomly places itself on lock/Aim
The Problem while not aiming
Script :
-----------<| SERVICES |>-----------
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local TweenService = game:GetService("TweenService")
-----------<| SERVICES |>-----------
-----------<| PLAYER |>-----------
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Mouse = LocalPlayer:GetMouse()
local Camera = Workspace.CurrentCamera
-----------<| PLAYER |>-----------
-----------<| FOLDERS |>-----------
local EVENTS = ReplicatedStorage:WaitForChild("EVENTS")
-----------<| FOLDERS |>-----------
-----------<| MODULES |>-----------
local PlayerModule = script.Parent:WaitForChild("PlayerModule")
local CameraModule = PlayerModule:WaitForChild("CameraModule")
local CameraUtils = require(CameraModule:WaitForChild("CameraUtils"))
-----------<| MODULES |>-----------
-----------<| REMOTES |>-----------
local Fire_Tool = EVENTS:WaitForChild("Fire_Tool")
local Reload_Tool = EVENTS:WaitForChild("Reload_Tool")
-----------<| REMOTES |>-----------
-----------<| LOCAL_SETTINGS |>-----------
local HasTool = script.HasTool
local Clicked = false
local isAiming = script.isAiming
local TweenInfo_CameraOffset = TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local CameraOffset_X = 0 -- Right/Left
local CameraOffset_Y = 0.5 -- Up/Down
local CameraOffset_Z = 0 -- GOT NO IDEA YET
-----------<| LOCAL_SETTINGS |>-----------
--[[
---------------------------------------------------------------------
FUNCTIONS
---------------------------------------------------------------------
--]]
function Check_ForTools()
if Character:FindFirstChild("GUN") or Character:FindFirstChild("TOOL") or Character:FindFirstChild("CONSUMABLE") then
HasTool.Value = true
else
HasTool.Value = false
end
end
function Change_Mouse_Icon()
Mouse.TargetFilter = Character
if HasTool.Value == true then
UserInputService.MouseIcon = "rbxassetid://6976265170"
elseif HasTool.Value == false then
UserInputService.MouseIcon = "rbxassetid://6933336998"
end
end
function Set_Offset()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid.CameraOffset == Vector3.new(0, 0, 0) then
local CameraOffset_Tween =
TweenService:Create(Humanoid, TweenInfo_CameraOffset,
{CameraOffset = Vector3.new(CameraOffset_X, CameraOffset_Y, CameraOffset_Z)})
CameraOffset_Tween:Play()
end
end
function Check_RotateCameraWithMouse()
local function Mouse_Fully_Follow_Camera()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserGameSettings.RotationType = Enum.RotationType.CameraRelative
end
local function Mouse_Only_Follow_Camera()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserGameSettings.RotationType = Enum.RotationType.MovementRelative
end
local function Mouse_Not_Follow_Camera()
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserGameSettings.RotationType = Enum.RotationType.MovementRelative
end
if HasTool.Value == true and isAiming.Value == true then
Mouse_Fully_Follow_Camera() -- true
elseif HasTool.Value == true and isAiming.Value == false then
Mouse_Only_Follow_Camera() -- true
elseif HasTool.Value == false and isAiming.Value == true then
Mouse_Not_Follow_Camera() -- false
elseif HasTool.Value == false and isAiming.Value == false then
Mouse_Not_Follow_Camera() -- false
end
end
--[[
---------------------------------------------------------------------
FUNCTIONS
---------------------------------------------------------------------
--]]
--[[
---------------------------------------------------------------------
MAIN_FUNCTION
---------------------------------------------------------------------
--]]
HasTool.Changed:Connect(function()
Change_Mouse_Icon()
Check_RotateCameraWithMouse()
end)
isAiming.Changed:Connect(function()
Change_Mouse_Icon()
Check_RotateCameraWithMouse()
end)
RunService.Stepped:Connect(function(deltaTime)
Check_ForTools()
Set_Offset()
end)
Mouse.Button1Down:Connect(function()
if not Clicked then
Clicked = true
--PUT EVENT HERE
task.wait(0.05) -- to not throttle RemoteEvents
Clicked = false
elseif Clicked then
warn(LocalPlayer.Name.." Please Stop Throttling Remote Event")
end
end)
Mouse.Button2Down:Connect(function()
isAiming.Value = true
end)
Mouse.Button2Up:Connect(function()
isAiming.Value = false
end)
--[[
---------------------------------------------------------------------
MAIN_FUNCTION
---------------------------------------------------------------------
--]]
-----------<| SERVICES |>-----------
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
-----------<| SERVICES |>-----------
-----------<| PLAYER |>-----------
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Mouse = LocalPlayer:GetMouse()
local Camera = Workspace.CurrentCamera
-----------<| PLAYER |>-----------
-----------<| FOLDERS |>-----------
local EVENTS = ReplicatedStorage:WaitForChild("EVENTS")
-----------<| FOLDERS |>-----------
-----------<| MODULES |>-----------
local PlayerModule = script.Parent:WaitForChild("PlayerModule")
local CameraModule = PlayerModule:WaitForChild("CameraModule")
local CameraUtils = require(CameraModule:WaitForChild("CameraUtils"))
-----------<| MODULES |>-----------
-----------<| REMOTES |>-----------
local Fire_Tool = EVENTS:WaitForChild("Fire_Tool")
local Reload_Tool = EVENTS:WaitForChild("Reload_Tool")
-----------<| REMOTES |>-----------
-----------<| LOCAL_SETTINGS |>-----------
local HasTool = script.HasTool
local Clicked = false
local isAiming = script.isAiming
local TweenInfo_CameraOffset = TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local tweenInfo1 = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local tweenInfo2 = TweenInfo.new(0.25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local CameraOffset_X = 0 -- Right/Left
local CameraOffset_Y = 0.5 -- Up/Down
local CameraOffset_Z = 0 -- Forward/Backward
local lastCameraMode
local MouseOffsetVertical = script.MouseOffsetVertical
local MouseLockOffsetValue = script.MouseLockOffsetValue
-----------<| LOCAL_SETTINGS |>-----------
--[[
---------------------------------------------------------------------
FUNCTIONS
---------------------------------------------------------------------
--]]
function Check_ForTools()
-- Wait for Character to load properly
Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
-- Check if the character has any tools
if Character:FindFirstChild("GUN") or Character:FindFirstChild("TOOL") or Character:FindFirstChild("CONSUMABLE") then
HasTool.Value = true
MouseOffsetVertical.Value = 8
MouseLockOffsetValue.Value = 23
else
HasTool.Value = false
MouseOffsetVertical.Value = 0
MouseLockOffsetValue.Value = 0
end
end
function Change_Mouse_Icon()
Mouse.TargetFilter = Character
if HasTool.Value then
UserInputService.MouseIcon = "rbxassetid://6976265170"
else
UserInputService.MouseIcon = "rbxassetid://6933336998"
end
end
function Update_Mouse_Lock_Offset(offsetX, offsetY, useQuickTween)
local tweenInfo = useQuickTween and tweenInfo2 or tweenInfo1
local offsetX_Tween = TweenService:Create(MouseOffsetVertical, tweenInfo, { Value = offsetY })
local offsetY_Tween = TweenService:Create(MouseLockOffsetValue, tweenInfo, { Value = offsetX })
offsetX_Tween:Play()
offsetY_Tween:Play()
end
function Change_User_Settings(newMode)
if lastCameraMode ~= newMode then
if newMode == "Default" then
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserGameSettings.RotationType = Enum.RotationType.MovementRelative
Update_Mouse_Lock_Offset(0, 0)
elseif newMode == "MouseRotate" then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserGameSettings.RotationType = Enum.RotationType.MovementRelative
Update_Mouse_Lock_Offset(23, 8)
elseif newMode == "MouseLock" then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserGameSettings.RotationType = Enum.RotationType.CameraRelative
Update_Mouse_Lock_Offset(27, 8)
end
lastCameraMode = newMode
end
end
function Check_RotateCameraWithMouse()
if HasTool.Value and isAiming.Value then
Change_User_Settings("MouseLock")
elseif HasTool.Value then
Change_User_Settings("MouseRotate")
else
Change_User_Settings("Default")
end
end
function Adjust_Camera_Position()
local objects = {
Character,
}
for _, player in ipairs(Players:GetChildren()) do
if player.Character then
table.insert(objects, player.Character)
end
end
local cutoffDistance = Camera:GetLargestCutoffDistance(objects)
if cutoffDistance > 0 then
Camera.CFrame = Camera.CFrame + Camera.CFrame.lookVector * cutoffDistance
return true
end
return false
end
function Update_Camera()
if Camera.CameraType == Enum.CameraType.Custom then
Check_RotateCameraWithMouse()
Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, 0)
local horizontalOffset = (Camera.CFrame.lookVector * Vector3.new(1, 0, 1)):Cross(Vector3.new(0, 1, 0)).unit * MouseLockOffsetValue.Value / 10
local verticalOffset = Vector3.new(0, MouseOffsetVertical.Value / 10, 0)
local offset = horizontalOffset + verticalOffset
Camera.CFrame = Camera.CFrame + offset
Adjust_Camera_Position()
Camera.Focus = Camera.Focus + offset
end
end
function Reset_Camera()
-- Optional: Reset camera if needed
end
-- Function to determine if RightMouseButton should be disabled
local function shouldDisableRightMouseButton()
return UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter and
UserGameSettings.RotationType == Enum.RotationType.CameraRelative
end
-- ContextActionService callback
local function rightMouseButtonCallback(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin and shouldDisableRightMouseButton() then
return Enum.ContextActionResult.Sink
end
return Enum.ContextActionResult.Pass
end
--[[
---------------------------------------------------------------------
MAIN_FUNCTION
---------------------------------------------------------------------
--]]
-- Connect changes
HasTool.Changed:Connect(function()
Change_Mouse_Icon()
Check_RotateCameraWithMouse()
end)
isAiming.Changed:Connect(function()
Change_Mouse_Icon()
Check_RotateCameraWithMouse()
end)
-- Update functions
RunService.Stepped:Connect(function(deltaTime)
Check_ForTools()
end)
Mouse.Button1Down:Connect(function()
if not Clicked then
Clicked = true
-- PUT EVENT HERE
task.wait(0.05) -- to not throttle RemoteEvents
Clicked = false
elseif Clicked then
warn(LocalPlayer.Name.." Please Stop Throttling Remote Event")
end
end)
Mouse.Button2Down:Connect(function()
isAiming.Value = true
end)
Mouse.Button2Up:Connect(function()
isAiming.Value = false
end)
-- Bind to RenderStep for camera updates
RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value + 1, Update_Camera)
RunService:BindToRenderStep("CameraReset", Enum.RenderPriority.Camera.Value - 1, Reset_Camera)
-- Bind the right mouse button action
ContextActionService:BindActionAtPriority(
"RightMouseDisable",
rightMouseButtonCallback,
false,
Enum.ContextActionPriority.Medium.Value,
Enum.UserInputType.MouseButton2
)