Returning to player camera not working

Hi, I am currently making a script using a camera for my main menu, but weirdly, it doesn’t work like it’s supposed to. I tried using the Studio’s AI Assistant thing but no matter how many times I’ve asked, it still doesn’t want to run. I’ve also searched everything in the DevForum, but still, none of them seem to work.

It consists of two scripts,
Here’s the main script (“Cam”, works just fine):

--overall sets the camera to its position; CamPart.

local camPart = workspace:WaitForChild("CamPart")
local camera = workspace:WaitForChild("Camera")
local gui = game.StarterGui:WaitForChild("MainMenu")

function tween(obj, dur, cmd)
    return game:GetService('TweenService'):Create(obj, dur, cmd)
end

workspace.Camera.CameraType = Enum.CameraType.Scriptable
local cameraTween = tween(camera, TweenInfo.new(3), {CFrame = camPart.CFrame})

cameraTween:Play()
cameraTween.Completed:Connect(function()
    game:GetService("RunService").RenderStepped:Connect(function()
        camera.CFrame = camPart.CFrame
    end)
end)

and here’s the 2nd script (“LocalScript”), which I am having trouble with:

local gui = game.StarterGui.MainMenu
local camPart = workspace:WaitForChild("CamPart")
local button =  script.Parent
local camera = workspace.Camera

-- return the camera to player
button.MouseButton1Click:Connect(function()
	--making sure it works
	print("returnplayer executed")
	
	gui.Enabled = false
	--returning camera to player
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	print("success")
end)

Now, weirdly, the print function works just fine, but hiding the GUI and returning the camera to the player doesn’t work.

Here’s the hierarchy of the properties


+CamPart located in Workspace

Here’s what it looks like:



FYI, there are no errors in the output. It just doesn’t want to return the camera and hide the GUI.

Waiting for your help!

Are they both local scripts or is one a server script and one a local script? Both scripts should be local. I am confused as to why you need two separate scripts which manage the same thing.

1 Like

Both are LocalScript. It was originally in the same script, but I purposefully divided it into 2 so it’s less confusing. But I guess that didn’t really work lol

1 Like

Okay, you want to do game.Players.localplayer.startergui instead of game.startgui

Looking over your code, you want to put them in the same local script. This is because to return the camera you want to disconnect the previous renderstepped connection

1 Like

In the following code I added in the disconnect function

--overall sets the camera to its position; CamPart.

local camPart = workspace:WaitForChild("CamPart")
local camera = workspace:WaitForChild("Camera")
local playerService = game:GetService("Players")
local player =playerService.localplayer
local playergui = player.PlayerGui
local gui = playergui:WaitForChild("MainMenu")

function tween(obj, dur, cmd)
    return game:GetService('TweenService'):Create(obj, dur, cmd)
end

workspace.Camera.CameraType = Enum.CameraType.Scriptable
local cameraTween = tween(camera, TweenInfo.new(3), {CFrame = camPart.CFrame})

cameraTween:Play()
local connection
cameraTween.Completed:Connect(function()
    connection = game:GetService("RunService").RenderStepped:Connect(function()
        camera.CFrame = camPart.CFrame
    end)
end)
local button =  script.Parent
local camera = workspace.Camera

-- return the camera to player
button.MouseButton1Click:Connect(function()
	--making sure it works
	print("returnplayer executed")
	
	gui.Enabled = false
	--returning camera to player
        connection:disconnect()
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	print("success")
end)```

I’m guessing you meant PlayerGui and not StarterGui? Since StarterGui doesn’t seem to exist for LocalPlayer

yes sry abt that. you are correct.

Okay, will test, give me a brief moment

try this code:

--overall sets the camera to its position; CamPart.

local camPart = workspace:WaitForChild("CamPart")
local camera = workspace:WaitForChild("Camera")
local playerService = game:GetService("Players")
local player =playerService.localplayer
local playergui = player.PlayerGui
local gui = playergui:WaitForChild("MainMenu")

function tween(obj, dur, cmd)
    return game:GetService('TweenService'):Create(obj, dur, cmd)
end

workspace.Camera.CameraType = Enum.CameraType.Scriptable
local cameraTween = tween(camera, TweenInfo.new(3), {CFrame = camPart.CFrame})

cameraTween:Play()
cameraTween.Completed:Connect(function()
    connection = game:GetService("RunService").RenderStepped:Connect(function()
        camera.CFrame = camPart.CFrame
    end)
end)
local button =  script.Parent

-- return the camera to player
button.MouseButton1Click:Connect(function()
	--making sure it works
	print("returnplayer executed")
	
	gui.Enabled = false
	--returning camera to player
        connection:disconnect()
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	print("success")
end)``````
1 Like

This statement is giving “Unknown global: connection”. Is it because there is no ‘connection’ variable in the first place?

Edit: sorry, I just realized you made a new variable. Will try.

try it now–i edited the code a bit.

Alright, it works. Thank you so much! :saluting_face:

1 Like

No problem, Glad I was able to help :wink:

1 Like

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