Camera system making character movement bug out

i was trying to make a top view camera controller of some sort and uhhhhhhh
it does this when i tab out and tab back in, i have no idea why
RobloxPlayerBeta_Ivnjd40Kj5
basically it dosent let me walk anymore and just walks in random directions, this is not related to the mouse rotation since it appeared way before i made that system, but originally i thought this was a studio only bug since it didnt appear in the main game

does anyone have any idea why this is happening? here is the code i use for the camera:

local Camera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local CamDelay = 0.2
local Mouse = game.Players.LocalPlayer:GetMouse()
Camera.CameraType = Enum.CameraType.Scriptable

function camsway()
	while true do
		task.wait()
		local HRP = Char:FindFirstChild("HumanoidRootPart")
		TweenService:Create(Camera, TweenInfo.new(CamDelay),{CFrame = CFrame.new(HRP.Position.X,HRP.Position.Y,HRP.Position.Z)*CFrame.new(0,45,0)*CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))}):Play()
	end
end

task.spawn(camsway)

RS.Heartbeat:Connect(function()
	local HRP = Char:FindFirstChild("HumanoidRootPart")
	HRP.CFrame = CFrame.new(HRP.Position, Vector3.new(Mouse.Hit.Position.X, HRP.Position.Y, Mouse.Hit.Position.Z))
end)

i dont have any idea why this is happening or what to even try to fix this, please help :sob:

5 Likes

Does it do this in game? I have had my local scripts, sounds, etc. stop working when I went to another window if I am using Studio.

1 Like

You’re setting the character’s HumanoidRootPart CFrame as the Mouse.Hit position at the end of your script. If the camera is linked to your Character, what happens is that when you set the Character position, you move the Camera too, moving the mouse position as well, creating an infinite loop.

To fix this, you can simply disconnect the camera from the character by setting its CameraFocus and CameraSubject properties, as well as the CameraType.

1 Like

I would recommend using RS.RenderStepped instead of RS.Heartbeat.

RenderStepped fires before anything else, taking priority over other interfering code.

For a more in-depth thread on RunService scheduling, you can read through this post. It has some very helpful resources.

1 Like

like i said the mouse thing was not related cuz it happened before that was even there

renderstepped can be manipulated with fps unlockers to get an advantage though

try doing what i said anyway. The problem is most certainly caused by the camera being attached to the character.

This should not be a reason to not use RenderStepped at all. Both RenderStepped and Heartbeat run every frame. If your goal is to crack down on FPS unlocker abuse, you will need to take a different approach.

@ilGravi I believe your approach won’t do what you may be anticipating. The footage shows that the camera is already detached from the player’s character. Even if the camera was attached to the player, this would make no difference as only the rotation is being set, not the position.

Try using RenderStepped or BindToRenderStep. It will most likely solve your problem.

it actually did! i was using a normal loop here because i wanted to add some fallback to the camera, but it caused some issues so i’ll use this until i find a better way. thank you for your help!!

at first i thought it didnt but then it randomly did. strangely enough it didnt replicate 100% of the time ingame.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.