Resetting the camera back to the player's

Hi. I’m having trouble with my script, I’m not sure why as there’s no errors, but it’s just not working.
Basically, I have the camera set to a part when the player joins. There’s a play button, and when the button is clicked I’m trying to get it so that the camera resets back to the player. The camera also follows the cursor in the play screen. I’m not sure how to disable this but I can’t even get to the point of resetting it properly.
Here’s how it looks in-game.

This is the script that handles the disabling and enabling while on the play menu, aswell as resetting the camera.

local startergui = game:GetService('StarterGui')
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
	startergui.Dialog.Enabled = false
	startergui.Stamina.Enabled = false

	startergui.Blur.Disabled = true

	startergui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

script.Parent.MouseButton1Click:Connect(function()
	
	script.Parent.Parent:TweenPosition(UDim2.new(0, 0,10, 0), 'Out', 'Linear', 1)
		startergui.Dialog.Enabled = true
		startergui.Stamina.Enabled = true

		startergui.Blur.Disabled = false

	startergui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
		cam.CameraSubject = player.Humanoid
		cam.CameraType = Enum.CameraType.Custom 
		cam.CFrame = player.Head.CFrame
end)

This is the script that sets the camera, and makes it follow the cursor smoothly.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()
local run = game:GetService("RunService")

local cp = workspace:WaitForChild("Cam")
local cam = workspace.CurrentCamera
local maxDegree = math.rad(30)

repeat
	cam.CameraType = Enum.CameraType.Scriptable
until 	cam.CameraType == Enum.CameraType.Scriptable

run.RenderStepped:Connect(function()
	local np = Vector2.new(mouse.X, mouse.Y)
	local centre = Vector2.new(cam.ViewportSize.X/2, cam.ViewportSize.Y/2)
	local difference = np - centre
	
	local outcome = Vector2.new((difference.Y/centre.Y)*maxDegree, (difference.X/centre.X)*maxDegree)
	
	cam.CFrame = CFrame.Angles(-outcome.X, -outcome.Y,0) + cp.Position
end)

I’m very nooby, and I put this little system together using my limited knowledge & some tutorials. Everything else works except for the camera resetting.

2 Likes

Set the camera type back to Enum.CameraType.Custom after you press your button, and make sure to disconnect that renderstepped function

I did that here, after the button is pressed it tweens out, enables all the game’s gui and at the bottom it sets it back to Enum.CameraType.Custom. It doesn’t work.

1 Like

Once the button gets pressed do this
Here is an example

YourButton.MouseButton1Click:Connect(function() -- the Play Button gets Clicked
      game.Workspace.Camera.CameraType = Enum.CameraType.Custom
end)
1 Like

Yes but you need to disconnect the renderstepped function or the camera will stay at the part

I see. I was going to attempt that later on, I didn’t know that was the issue here. How would I even go about doing that? My first thought is to destroy the script after you click the button.

Use BindToRenderstep instead, and UnbindFromRenderStep when you press the button

You have 2 options to use either BindToRenderstep or HeartBeat, Here is an example of HeartBeat

local Connection = game.RunService.HeartBeat(Connect(function()
      
end)

Connection:Disconnect()

His function is in 2 different scripts, BindToRenderStep is the easiest way here

I am not familiar with renderstep at all, I got most of that from tutorials. How would I go about using it to fix my issue?

BindToRenderStep: RunService:BindToRenderStep

UnbindFromRenderStep: RunService:UnbindFromRenderStep

but if you want an example:

run:BindToRenderStep("MenuCamera", 0, function()
	local np = Vector2.new(mouse.X, mouse.Y)
	local centre = Vector2.new(cam.ViewportSize.X/2, cam.ViewportSize.Y/2)
	local difference = np - centre

	local outcome = Vector2.new((difference.Y/centre.Y)*maxDegree, (difference.X/centre.X)*maxDegree)

	cam.CFrame = CFrame.Angles(-outcome.X, -outcome.Y,0) + cp.Position
end)

and

run:UnbindFromRenderStep("MenuCamera")

I replaced my code with the example you gave, and put the Unbind after clicking the button. Is this how you meant for it to be used? Now, the camera is never set to where it’s supposed to be, and it gets stuck looking in one direction.

Try deleting the part where you set the camera’s cframe to the player’s head after you press the button, if that doesn’t work then try playing with the bind priority (the 0 in bind to render step)

That doesn’t fix it either, the camera still doesn’t get set to the Cam part inside the store, and pressing the play button doesn’t do anything either.

Could i see your code now? incase you did something wrong

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local run = game:GetService("RunService")

local cp = workspace:WaitForChild("Cam")

local cam = workspace.CurrentCamera

local maxDegree = math.rad(30)

repeat

cam.CameraType = Enum.CameraType.Scriptable

until cam.CameraType == Enum.CameraType.Scriptable

run:BindToRenderStep("MenuCamera", 0, function()

local np = Vector2.new(mouse.X, mouse.Y)

local centre = Vector2.new(cam.ViewportSize.X/2, cam.ViewportSize.Y/2)

local difference = np - centre

local outcome = Vector2.new((difference.Y/centre.Y)*maxDegree, (difference.X/centre.X)*maxDegree)

cam.CFrame = CFrame.Angles(-outcome.X, -outcome.Y,0) + cp.Position

end)

game.StarterGui.WelcomeIntroGui.Welcome.Play.MouseButton1Click:Connect(function()

run:UnbindFromRenderStep("MenuCamera")

end)

I got rid of the need to go between two scripts, they are now completely separate. Everything regarding the camera is in this script.

you still need everything else inside where the button is pressed

local startergui = game:GetService('StarterGui')
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
	startergui.Dialog.Enabled = false
	startergui.Stamina.Enabled = false

	startergui.Blur.Disabled = true

	startergui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

script.Parent.MouseButton1Click:Connect(function()
	
	script.Parent.Parent:TweenPosition(UDim2.new(0, 0,10, 0), 'Out', 'Linear', 1)
		startergui.Dialog.Enabled = true
		startergui.Stamina.Enabled = true

		startergui.Blur.Disabled = false

	startergui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
end)

you mean everything in this script?

1 Like

Also, since everything is now in the same script, you could just do normal renderstep and disconnect the reference when the button is pressed

yes everything in script.parent.mousebutton1click