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)
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.
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: