Need help with fixing a scirpt

Hi so basicaly i found this script on devforum and i modified it so it will work as i need but now its glitched randomly when i rejoin it does this glitch that i will show in the video but its not supposed to work it should rotate everywhere and not like in this

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera

local sensitivityDivisor = 50 -- Increased divisor to slow down the rotation
local maxRotationPerFrame = math.rad(2) -- Limits the maximum rotation per frame to avoid fast spinning
local lastMousePos = Vector2.new(0, 0)

-- Function to set up camera control
local function setupCameraControl()
	-- Lock the mouse in the center of the screen
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

	-- Get the current mouse position to initialize lastMousePos
	lastMousePos = UIS:GetMouseLocation()

	-- Function to calculate mouse movement delta
	local function calculateMouseDelta()
		local currentMousePos = UIS:GetMouseLocation()
		local delta = currentMousePos - lastMousePos
		lastMousePos = currentMousePos
		return delta
	end

	-- Function to point the camera based on mouse movement
	local function pointCameraToMouse(input: InputObject)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local delta = calculateMouseDelta()

			-- Calculate the desired rotation amount and apply a limit to prevent fast spinning
			local rotationX = math.clamp(math.rad(delta.Y / -sensitivityDivisor), -maxRotationPerFrame, maxRotationPerFrame)
			local rotationY = math.clamp(math.rad(delta.X / -sensitivityDivisor), -maxRotationPerFrame, maxRotationPerFrame)

			-- Rotate the camera smoothly
			Camera.CFrame *= CFrame.Angles(rotationX, rotationY, 0)
		end
	end

	-- Connect mouse movement to camera rotation
	UIS.InputChanged:Connect(pointCameraToMouse)
end

-- Function to handle player respawn or character addition
local function onCharacterAdded(character)
	-- Wait for the camera to reset properly
	wait(0.5)
	Camera = workspace.CurrentCamera
	setupCameraControl()
end

-- Get the local player and connect CharacterAdded to handle resets
local player = Players.LocalPlayer
player.CharacterAdded:Connect(onCharacterAdded)

-- Call setupCameraControl when the script runs for the first time
setupCameraControl()

How it shouild work

when its glitched

sorry if the videos looks small

1 Like

The script seems to work correctly when I test it in a new baseplate, even after I reset my character, so the issue could be happening due to a different script

it’s most likely due to Camera being assigned a new instance upon rejoining and workspace.CurrentCamera is reset, you just need to update Camera everytime a player character gets added

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local sensitivityDivisor = 50 -- Increased divisor to slow down the rotation
local maxRotationPerFrame = math.rad(2) -- Limits the maximum rotation per frame to avoid fast spinning
local lastMousePos = Vector2.new(0, 0)

-- Function to set up camera control
local function setupCameraControl()
	-- Lock the mouse in the center of the screen
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

	-- Get the current mouse position to initialize lastMousePos
	lastMousePos = UIS:GetMouseLocation()

	-- Function to calculate mouse movement delta
	local function calculateMouseDelta()
		local currentMousePos = UIS:GetMouseLocation()
		local delta = currentMousePos - lastMousePos
		lastMousePos = currentMousePos
		return delta
	end

	-- Function to point the camera based on mouse movement
	local function pointCameraToMouse(input: InputObject)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local delta = calculateMouseDelta()

			-- Calculate the desired rotation amount and apply a limit to prevent fast spinning
			local rotationX = math.clamp(math.rad(delta.Y / -sensitivityDivisor), -maxRotationPerFrame, maxRotationPerFrame)
			local rotationY = math.clamp(math.rad(delta.X / -sensitivityDivisor), -maxRotationPerFrame, maxRotationPerFrame)

			-- Rotate the camera smoothly
			local camera = workspace.CurrentCamera
			if camera then
				camera.CFrame *= CFrame.Angles(rotationX, rotationY, 0)
			end
		end
	end

	-- Connect mouse movement to camera rotation
	UIS.InputChanged:Connect(pointCameraToMouse)
end

-- Function to handle player respawn or character addition
local function onCharacterAdded(character)
	-- Wait for the camera to reset properly
	wait(0.5)
	setupCameraControl()
end

-- Get the local player and connect CharacterAdded to handle resets
local player = Players.LocalPlayer
player.CharacterAdded:Connect(onCharacterAdded)

-- Call setupCameraControl when the script runs for the first time
if player.Character then
	onCharacterAdded(player.Character)
end

the thing is that i dont have any other scirpts except the scirpt animate that changes animations

it kinda works but its still sometimes hve the same glitch

This should work, but make sure that it’s in StarterCharacterScripts, not StarterPlayerScripts:

-- LocalScript in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")

local SENSITIVITY_DIVISOR = 50
local MAX_ROTATION_PER_FRAME = math.rad(2)

local camera = Workspace.CurrentCamera or Workspace:WaitForChild("Camera")

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter


UserInputService.InputChanged:Connect(function(inputObject: InputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		local mouseDelta = UserInputService:GetMouseDelta()

		local rotationX = math.clamp(-math.rad(mouseDelta.X / SENSITIVITY_DIVISOR), -MAX_ROTATION_PER_FRAME, MAX_ROTATION_PER_FRAME)
		local rotationY = math.clamp(-math.rad(mouseDelta.Y / SENSITIVITY_DIVISOR), -MAX_ROTATION_PER_FRAME, MAX_ROTATION_PER_FRAME)

		camera.CFrame *= CFrame.Angles(rotationX, rotationY, 0)
	end
end)

it works but sometimes it randomly just completly stop working when i rejoin the game it kinda like first time i join it works i rejoin again it stops wrking i rejoin the third it starts working again

and yes i did tried in another game

1 Like

This is quite strange because I’m not experiencing any issues on my end, both when testing the original script and the one I made. You could be experiencing some kind of bug, or there’s also the possibility that another program could be interfering with your mouse


@super_marvel1232

Note that I’m testing them using Studio, not the Player. This doesn’t change the fact that a script that seems to be working correctly in Studio shouldn’t be experiencing this kind of a problem in the Player, so I’d still report it as a bug

1 Like

hmm ok thanks for trying though!

1 Like