So I am trying to build a racing game and I want players to have a first person perspective when in the car which is defined by a part that I am using as my 'camera'. Here is what I've done so far:
local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local cam = game.Workspace.CurrentCamera local camPart = game.Workspace["Lamborghini Aventador-J"].Body.Camera camPart.Entered.OnClientEvent:Connect(function() --camPart.Entered is a remote event repeat wait() cam.CameraType = Enum.CameraType.Scriptable until cam.CameraType == Enum.CameraType.Scriptable cam.CFrame = camPart.CFrame end)
This does work, but the problem is that when we drive the car, the player's camera perspective is left behind and the car drives off with the camPart. I think a good way to solve this might be to detect when the camPart is in motion with the car and set the CFrame of cam to the camPart whenever the script thinks it's moving. I just don't know how I'd detect the camPart moving.
Any ideas?
I have an alternative suggestion. Set the Camera's CFrame to the part using RunService, so your code will condense to this:
local RunServiceFunction = game:GetService("RunService").RenderStepped:Connect(function() Camera.CameraType = Enum.CameraType.Scriptable Camera.CFrame = Campart.CFrame end)
You can do RunServiceFunction:Disconnect()
when you want it to stop.
Also, I'm not sure if RenderStepped is working currently because Roblox is replacing it with PreRender (you'll just have to substitute RenderStepped with PreRender).