Camera changing on pressing buttons

my script working, but when i press any buttons camera coordinate frame changing.

cam = workspace.CurrentCamera;
pal = game.Players.LocalPlayer.Character;
deb = false;
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local l__RenderStepped__1 = game:GetService("RunService").RenderStepped;

function dead(inputObject, gameProcessedEvent)
	if pal.Humanoid.Health == 0 then
		deb = not deb;
		if deb == true then
			cam.CameraType = "Custom";


			while true do
				cam.CoordinateFrame = pal.Head.CFrame * CFrame.new(0, 0, -0.6);
				cam.FieldOfView = 86
				UserInputService.MouseIconEnabled = false
				l__RenderStepped__1:wait();
				if deb == false then
					UserInputService.MouseIconEnabled = true
					break;

				end;		
			end;
			cam.CameraType = "Custom";


		end;
	end
end;
pal.Humanoid.Changed:connect(function()
	if pal.Humanoid.Sit == true then
		deb = true;
	end;
end);

game:GetService("UserInputService").InputBegan:connect(dead)

how it looks in game:
robloxapp-20240225-1744186.wmv (2.0 MB)

Why are you using InputService? You’re also swapping debounce from true to false and vice versa every keypress, so when your script notices that the debounce is false it resets the script.

1 Like

the script stops working if I remove unnecessary things.

cam = workspace.CurrentCamera;
pal = game.Players.LocalPlayer.Character;
local player = game.Players.LocalPlayer

local function dead()
	if pal.Humanoid.Health == 0 then
			cam.CameraType = "Custom"
				cam.CoordinateFrame = pal.Head.CFrame * CFrame.new(0, 0, -0.6);
	end;
end

dead()

You need to set CameraType to Scriptable. Also, CoordinateFrame is an outdated method of Camera. Use CFrame instead. Try this:

cam = workspace.CurrentCamera;
pal = game.Players.LocalPlayer.Character;
local player = game.Players.LocalPlayer

local function dead()
	if pal.Humanoid.Health == 0 then
		cam.CameraType = "Scriptable"
		cam.CFrame = pal.Head.CFrame * CFrame.new(0, 0, -0.6);
	else
		cam.CameraType = "Custom"
	end;
end

dead()

I found the solution!
i changed the last line and it worked.

cam = workspace.CurrentCamera;
pal = game.Players.LocalPlayer.Character;
deb = false;
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local l__RenderStepped__1 = game:GetService("RunService").RenderStepped;

function dead()
if pal.Humanoid.Health == 0 then
		deb = not deb;
		if deb == true then
			while true do
				cam.CFrame = pal.Head.CFrame * CFrame.new(0, 0, -0.6);
				cam.FieldOfView = 86
				l__RenderStepped__1:wait();		
         end
      end
   end
end
pal:WaitForChild("Humanoid").Died:connect(dead)
1 Like

Ahh, can’t believe I didn’t catch that! Well, I’m glad it works.

1 Like