Camera not changing on button click

I want to change the camera back to the player after a GUI button is clicked but even after clicking the
button the camera is not changing
The code:

				local function OnChanged()
			
							if fin == true then
								camera.CameraSubject = character
							camera.CameraType = "Custom"
		
							end
							if fin == false then
								camera.CFrame =  campart.CFrame
							end

				
				end
				Run.RenderStepped:Connect(OnChanged)
					script.Parent.exit.MouseButton1Click:Connect(function(player)

						fin = true
					end)

SS:

1 Like

I think there’s a better method to achieve what you want here, I would suggest not using a renderStepped based check if not necessary. Also, why don’t you pass fin as an argument to your function? Maybe something like this:

local Out = false -- Global Variable

local function OnChanged(fin)
    Out = fin -- so the global variable can update.
    if fin then -- this is the same is "if fin == true then"
	    camera.CameraSubject = character
	    camera.CameraType = "Custom"
    else -- there is only one other possible scenario, fin is equal to false
        camera.CameraType = "Scriptable" -- So we can position cam
        repeat -- Keep this loop going until "Out" == true so the camera is always being positioned
            Run.RenderStepped:Wait()
            camera.CFrame = campart.CFrame
        until Out --Same as writing Out == true
    end
end

script.Parent.exit.MouseButton1Click:Connect(function()
    OnChanged(true)--Call the function, pass through false.
end)

When you enter the car, you will want to call OnChanged(false) to initially snap the camera to position, the repeat loop will then wait based on renderstepped until the player hops out (Out = false) and the camera will go back to the player again.

2 Likes

This does work but there is also some GUI tweening code below this code and when I call the onchanged function with the false argument it does keep changing the camera position but the other GUI tweening scripts don’t run which make it appear stuck like this:


Below is the GUI tweening code,

wait(1)
					camera.CFrame = campart.CFrame
					script.Parent.TextLabel.Visible = true
					script.Parent.TextLabel.Text = "3"
					wait(1)
					script.Parent.TextLabel.Text = "2"
					wait(1)
					script.Parent.TextLabel.Text = "1"
					wait(1)
					script.Parent.TextLabel.Text = "GO!"
					script.Parent.Frame.Frame.Size = UDim2.new(1,0,1,0)
					script.Parent.Frame.Frame.Visible = true
					Exit.Visible = true
					script.Parent.Frame.Visible = true
					script.Parent.Frame.TextLabel.Text = fuel.."/"..fuel
					local currentvalue = fuel
					wait(0.5)
					script.Parent.TextLabel.Visible = false
					OnChanged(false)
					repeat 
						currentvalue = currentvalue - 1
						script.Parent.Frame.TextLabel.Text = currentvalue.."/"..fuel
						local percent = currentvalue/fuel
						script.Parent.Frame.Frame.Size = UDim2.new(percent,0,1,0)
						camera.CFrame = campart.CFrame
						wait(0.1)
				
					until currentvalue == 0
			
					if currentvalue == 0 then
						script.Parent.Frame.Frame.Visible = false
						wait(1)
						fin = true
						script.Parent.Cash.Visible = true
						script.Parent.Cash.Text = "You earned "..math.ceil((fuel/10)*10*cash_multiplier).." cash!"
						wait(2)
						OnChanged(true)
						Exit.Visible = false
						script.Parent.Cash.Visible = false
						script.Parent.Frame.Visible = false
						script.Parent.Frame.Frame.Size = UDim2.new(1,0,1,0)
					end