[FIXED] I can't reset the camera correctly

Hello!

  1. What do you want to achieve? Keep it simple and clear!
    I am currently working on a Train Spawner GUI. When you enter the GUI, the camera changes of Position. And if i exit the GUI, the camera needs to reset.
  2. What is the issue? Include screenshots / videos if possible!
    It resets to the player, but when i try to turn the camera, it stills stuck.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I did look, but i didn’t find anything.
    I tried to change the camera type to Custom, camera subject to the player’s Humanoid and the CFrame to the Head’s CFrame

Here’s the codes i used:

-- Code that change the Camera position:
local ts = game:GetService("TweenService")

local ex = script.Parent.Frame.trainchoose.exit.ex -- my exit button variable

game.ReplicatedStorage.train.OnClientEvent:Connect(function(plr)
	-- animate the rear cam
	
	local part = workspace.spacam
	local part2 = workspace.spacam2
		
	local ti = TweenInfo.new(
		10,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		-1,
		true,
		0
	)
	----------------------------------------------------------
	local tw = ts:Create(part,ti,{CFrame = part2.CFrame})
	tw:Play()
	ex.MouseButton1Click:Connect(function() -- i didn't know how to cancel the tweening in the other script
		tw:Cancel()
	end)
	workspace.Camera.CameraSubject = workspace.spacam
	workspace.Camera.CameraType = Enum.CameraType.Scriptable
	while wait() do
		workspace.Camera.CFrame = part.CFrame
	end
	
end)
-- And the code that resets the camera
script.Parent.MouseButton1Click:Connect(function(plr)
	local h = game.Players.LocalPlayer.Character.Humanoid -- humanoid variable
	wait(1)
	workspace.Camera.CameraType = Enum.CameraType.Follow
	workspace.Camera.CameraSubject = h
	workspace.Camera.CFrame = h.Parent:FindFirstChild("Head").CFrame
end)

Thanks in advance if you help me!

(This is my first post so if i did something wrong, please tell me!)

I dont think its cancel, i think its tween:Pause().

Also, the default camera is Custom. Your code looks like this:

workspace.Camera.CameraType = Enum.CameraType.Follow

when it should look like:

workspace.Camera.CameraType = Enum.CameraType.Custom

also, the

workspace.Camera.CFrame = h.Parent:FindFirstChild("Head").CFrame

shouldn’t be necessary. :happy1:

1 Like

Thank you, i’ll try that! I just started learning camera.

1 Like

Nope, wasn’t working! Sorry. 30chars

hmmm. is there any other code controlling the camera you can show me?

No. But when the gui is showing up, the player is being teleported under the baseplate. That’s maybe the problem…

Maybe, but i dont think parts can control the camera in the way that the video was showing it.

Here’s a full video that could help you.

I see a fix cam button. Did you even press it to try to fix the camera?

Thats what your mousebutton code was probably attached to.

Then what was that last bit of code for??

It wasn’t for the Exit button because there would have had to been more code to close the gui.

Oh yeah, i just wanted to make a clear script, but here’s the full script:

local ts = game:GetService("TweenService")
local sc = script.Parent.Parent.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function(plr)
	local h = game.Players.LocalPlayer.Character.Humanoid
	-- add the bar
	wait()
	pcall(function()
		local starterGui = game:GetService('StarterGui')
		starterGui:SetCore("TopbarEnabled", true)
	end)
	wait()
	for i,v in pairs(sc:GetChildren())do
		v.Enabled = true
	end
	local gui = sc.trainguispa.Frame
	gui:TweenPosition(UDim2.new(0,0,1,0),nil,Enum.EasingStyle.Quart,3.2)
	----------------------------------------------------------
	workspace.Camera.CameraType = Enum.CameraType.Custom
	workspace.Camera.CameraSubject = h
	local frame = gui.trainchoose
	frame:TweenPosition(UDim2.new(0.196, 0, 1, 0),nil,Enum.EasingStyle.Quart,2.6)
	wait(3.200001)
	sc.trainguispa.Enabled = false
	
end)

Oh. I don’t know why it couldn’t be working then. :frowning:

Thank you for helping me anyways

Did you try turning the camera mode to custom?

Yes. Thats already in his code.

I think the CFrame has something to do with the wrong resetting (I may be wrong)

I think too, i should maybe try to reset the CFrame and then the CameraType.

1 Like

I fixed it! As @AzimuthBecameReal said it, it’s a CFrame problem. The problem was

while wait() do
		workspace.Camera.CFrame = part.CFrame
end

It’s a loop, so it doesn’t stop. I fixed it by disabling this script (i don’t really know a lot on loops)

-- Camera reset code
local ts = game:GetService("TweenService")
local sc = script.Parent.Parent.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function(plr)
	local h = game.Players.LocalPlayer.Character.Humanoid
	-- add the bar
	wait()
	pcall(function()
		local starterGui = game:GetService('StarterGui')
		starterGui:SetCore("TopbarEnabled", true)
	end)
	wait()
	for i,v in pairs(sc:GetChildren())do
		v.Enabled = true
	end
	local gui = sc.trainguispa.Frame
	gui:TweenPosition(UDim2.new(0,0,1,0),nil,Enum.EasingStyle.Quart,3.2)
	---------------------------------------------------------- 
-- Fix:
	wait(.1)
	sc.trainguispa.LocalScript.Disabled = true
---------------------------------------------------------- 
	workspace.Camera.CameraType = Enum.CameraType.Custom
	workspace.Camera.CameraSubject = h
	local frame = gui.trainchoose
	frame:TweenPosition(UDim2.new(0.196, 0, 1, 0),nil,Enum.EasingStyle.Quart,2.6)
	wait(3.200001)
	sc.trainguispa.Enabled = false
	
end)

Thank you for helping me!

2 Likes