Camera not returning to localplayer

I’m manipulating the camera for my main menu, and when the ‘Play’ button is pressed, it returns to normal.

Script:

local cam = workspace.CurrentCamera
local oldpos = cam.CFrame
local db = false
if db == false then
	db = true
	print("e")
	local player = game.Players.LocalPlayer

	repeat wait() until player.Character

	cam.CameraType = Enum.CameraType.Track
	repeat
		cam.CFrame = workspace:WaitForChild("MenuCam1").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam1")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam2").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam2")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam3").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam3")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam4").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam4")
	until script.Parent.Frame.Play.MouseButton1Click
	print("Pressed")
end

script.Parent.Frame.Play.MouseButton1Click:Connect(function()
	cam.CameraType = Enum.CameraType.Custom
	cam.CameraSubject =  game.Players.LocalPlayer.Character
	cam.CFrame = oldpos
end)

However, it doesn’t return to normal, it stays in the old position, but does give me the ability to look around.

You can’t repeat until a RBXScriptSignal fires, because it will never be equal to a Boolean. The closest you can get is :Wait(), which might be a bit difficult to implement in this situation.

I’d suggest using a Boolean to control whether the loop for the camera can run or not, and when the Boolean gets set to false, the loop breaks. The Boolean will get set to false in the procedure connected to MouseButton1Click (which you could just use :Once for, if this only happens on players joining).

local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local canLoop = true

task.spawn(function()
    while canLoop do
        --your camera code here
    end
end)

local function exit()
    canLoop = false

    cam.CameraType = Enum.CameraType.Custom
	cam.CameraSubject = humanoid --CameraSubject must be a BasePart or a Humanoid, not a model.
	cam.CFrame = oldpos
end

script.Parent.Frame.Play.MouseButton1Click:Once(exit)

It seems to work for awhile, but after some time passes, it switches back to the menu camera. I tried playing around with the canLoop a bit, but still I did not get to fix it, could you help me?

Can you send your updated script? It might be difficult to help otherwise if many changes have been made.

local cam = workspace.CurrentCamera
local oldpos = cam.CFrame
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local canLoop = true

task.spawn(function()
	while canLoop do
		cam.CFrame = workspace:WaitForChild("MenuCam1").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam1")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam2").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam2")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam3").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam3")
		wait(10)
		cam.CFrame = workspace:WaitForChild("MenuCam4").CFrame
		cam.CameraSubject = workspace:WaitForChild("MenuCam4")
	end
end)

local function exit()
	canLoop = false

	cam.CameraType = Enum.CameraType.Custom
	cam.CameraSubject = humanoid --CameraSubject must be a BasePart or a Humanoid, not a model.
	cam.CFrame = oldpos
end

script.Parent.Frame.Play.MouseButton1Click:Connect(exit)

I tried changing :Once() to :Connect() which also gave the same result.

I think it snaps back to the old camera because of the Wait() functions

It’s because you have so much wait time between when the iteration actually checks the canLoop variable. You need to find a way to get around that.

One way could be adding statements throughout the loop:

if not canLoop then break end

also, use task.wait() instead of wait(), it’s more accurate because it’s not subject to throttling.

I’ll just simplify it for now by only choosing 1 randomly selected camera. Thanks for your help

1 Like

It’s quite hard to fully solve this issue without drastically changing the code or adding a lot of those statements which I put in my previous post. Glad I could help, though.

Use :Once here, it just disconnects the connection after it has been used once to help preserve memory.

1 Like

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