Unable to reset player's camera

My menu is not changing back to the player’s camera when I have coded it to.

  1. There is a script in StarterPlayer that is changing the camera to scriptable, one time, and there is a Menu Mouse / Camera Movement code that doesn’t really affect it and when manually I delete the camera and then change it to Custom, and subject to Humanoid it works.

Code:

local TweenService = game:GetService("TweenService")
local Team = game:GetService("Teams")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local tweenInfo2 = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local PlayButton = script.Parent
local TeamUI = script.Parent.Parent.TeamSelectFrame
local TeamD = TeamUI.TeamD
local TeamSECURE = TeamUI.TeamSECURE
local TeamScientist = TeamUI.TeamScientist
local MainFrame = script.Parent.Parent.MainFrame
local Player = game.Players.LocalPlayer
local Players = game:GetService("Players")
local tweenPlayButton = TweenService:Create(PlayButton, tweenInfo, {Position = UDim2.new(0.389, 0, 0.31, 0)})
local tweenTeamFrame1 = TweenService:Create(TeamUI, tweenInfo, {Position = UDim2.new(0.566, 0, 2, 0)})
local tweenMainFrame1 = TweenService:Create(MainFrame, tweenInfo, {Position = UDim2.new(0.103, 0,2, 0)})

local ScientistPart = game.Workspace.ScientistPart
local TestSubjectPart = game.Workspace.TestSubjectPart

local Camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")

TeamSECURE.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Security
		end
	end
end)

TeamD.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams["Test Subject"]
			game.Players.LocalPlayer.Character:MoveTo(TestSubjectPart.Position)
		end
	end
end)

TeamScientist.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play() 
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Scientist
			game.Players.LocalPlayer.Character:MoveTo(ScientistPart.Position)
		end
	end
end)
PlayButton.MouseButton1Click:Connect(function()
	PlayButton.Visible = false
	repeat wait ()
		Camera.CameraType = Enum.CameraType.Custom
	until Camera.CameraType == Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	Camera.CameraSubject = Char.Humanoid
	Camera.CameraType = "Custom"
	Camera.CFrame = Char.Head.CFrame
end)

the camera part is near the end.

It may not be this script? I’ve tested this part of your code:

repeat wait ()
Camera.CameraType = Enum.CameraType.Custom
   until Camera.CameraType == Enum.CameraType.Custom
Camera.CameraSubject = Humanoid
Camera.CameraSubject = Char.Humanoid
Camera.CameraType = "Custom"
Camera.CFrame = Char.Head.CFrame

and it seems to work

If it helps, the code is placed in a local script in StarterGui

Yeah, that won’t change much. By the way don’t use wait use task.wait(). It for sure isn’t this script can I see your other one that changes the camera?

LocalScript in StarterPlayerScripts

local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable

Camera Mouse Movement is also in StarterPlayerScripts

local mouse = game.Players.LocalPlayer:GetMouse()
local cam = workspace.CurrentCamera
local camPart = workspace.MenuCam

local Scale = 5000

cam.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function()
	local center = Vector2.new(cam.ViewportSize.X/2, cam.ViewportSize.Y/2)
	local x = mouse.X - center.X / 2
	local y = mouse.Y - center.Y / 2
	local xOffset = x/Scale 
	local yOffset = y/Scale

	local lookAtPoint = camPart.Position+camPart.CFrame.LookVector*5
	local vector = Vector3.new(
		lookAtPoint.X - xOffset,
		lookAtPoint.Y - yOffset,
		lookAtPoint.Z - xOffset)  

	local result = CFrame.lookAt(camPart.CFrame.Position,vector)

	cam.CFrame = result
end)

Menu Code for setting it to the part LocalScript that is located in StarterGui

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait ()
local Camera = workspace.CurrentCamera

repeat wait ()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = game.Workspace.MenuCam.CFrame
1 Like

Hi, sorry for the late reply I was busy. It’s because you aren’t disconnecting the renderstepped connection.

Again, avoid wait it’s not good instead use task.wait(). Look at this post it explains it well.

So i just change it to repeat task.wait()?

Is the Script inside StarterGui?

Yes, it is in StarterGui. It is a local script.
I revised the code by destroying the current camera, when done manually works and does work in the code, but does not then change the camera subject and type
I tried adding a wait to it so it would not change it before or while it is being :Destroy(d) but that does not work.

PlayButton.MouseButton1Click:Connect(function()
	PlayButton.Visible = false
	game.Workspace.CurrentCamera:Destroy()
	task.wait(0.1)
repeat task.wait ()
		Camera.CameraType = Enum.CameraType.Custom
	until Camera.CameraType == Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CFrame = Char.Head.CFrame
end)