How to move the camera back to the player

Hello!
As you can see by the title I can’t move the camera back to the player
When connected to the game, the main menu will appear and there the player clicks on Play and he should teleport to “Spawn” and the camera should also return, but I don’t understand how to do it!

here is the Camera code (Menu script camera):



local Player = game.Players.LocalPlayer 
local Character = Player.Character 
local Camera = workspace.CurrentCamera 
local Mouse = game.Players.LocalPlayer:GetMouse() 
local DefaultCFrame = workspace.MenuCameraPart.CFrame 
local Scale = 500
local Background = script.Parent.MenuSelect 
local PlayButton = Background.PlayButton 
local InfoButton = Background.InformationButton 
local SettingButton = Background.SettingsButton 
local TweenService = game:GetService("TweenService")




repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function()
	Camera.Focus = DefaultCFrame
	local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
	local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
	Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))
	Camera.FieldOfView = 83 
end) 

Camera.CFrame = workspace.MenuCameraPart.CFrame 

and here is the code that is attached to the “TextButton”:

local UIS = game:GetService("UserInputService")
local button = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

button.MouseButton1Up:Connect(function()
	script.Parent.Parent.Parent.Parent.Enabled = false
	Player.Character.HumanoidRootPart.Anchored = false
	Player.Character.HumanoidRootPart.CFrame = game.Workspace.Spawn.CFrame

	local camera = workspace.CurrentCamera
	camera.CFrame = Player.Character.HumanoidRootPart.CFrame
end)

Instead of doing

camera.CFrame = Player.Character.HumanoidRootPart.CFrame

Try

camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = Player.Character.Humanoid

Also make sure to disconnect the RenderStepped function or else the camera will just keep getting teleported.
You can disconnect the function if you set it up like this:

local Connection

function OnRenderStepped()
	Camera.Focus = DefaultCFrame
	local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
	local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
	Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))
	Camera.FieldOfView = 83 
end

Connection = game:GetService("RunService").RenderStepped:Connect(OnRenderStepped)

--Some kind of yield (when the TextButton is pressed)

Connection:Disconnect()

You need to reset the camera.CameraType to follow or another predefined type (i.e. not scriptable)
There is a list of types here:
Camera | Documentation - Roblox Creator Hub

I need to use the event, right? for renderstep

If you’re referring to when you connect the function, yes you use Runservice.RenderStepped.

Sorry, I’m a little stupid, I kind of wrote it correctly, but it seems like it’s wrong, can you check?

local Connection

function OnRenderStepped()
	Camera.Focus = DefaultCFrame
	local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
	local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
	Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))
	Camera.FieldOfView = 83 
end

Connection = game:GetService("RunService").RenderStepped:Connect(OnRenderStepped)

--Some kind of yield (when the TextButton is pressed)
event.OnClientEvent:Connect(function()
	Connection:Disconnect()
end)


Camera.CFrame = workspace.MenuCameraPart.CFrame 

here TextButton script:

local UIS = game:GetService("UserInputService")
local button = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Event = game.ReplicatedStorage.Event.ButtonPlay
button.MouseButton1Up:Connect(function()
	script.Parent.Parent.Parent.Parent.Enabled = false
	Player.Character.HumanoidRootPart.Anchored = false
	Player.Character.HumanoidRootPart.CFrame = game.Workspace.Spawn.CFrame
	
	Event:FireServer()
end)

I have solved this problem, i need use only one script for all guis ah

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