Car camera script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a simple script where if the player sits in a vehicle seat it will change the camera min zoom distance to prevent the player from looking into first person.
  2. What is the issue? Include screenshots / videos if possible!
    My issue is that I don’t know how to detect when the player has left the seat.
    Here is my current script:
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid: Humanoid = seat.Occupant
	local character: Model = humanoid.Parent
	local player: Player = game.Players:GetPlayerFromCharacter(character)
	if seat.Occupant then
		player.CameraMinZoomDistance = 15
	else
		player.CameraMinZoomDistance = 0.5
	end
end)

I PLACED THE SCRIPT INSIDE THE VEHICLE SEAT

If anybody can tell me a way to detect when a player has left the seat it would be really appreciatted!!

1 Like

I think you can only change the CameraMin/MaxZoomDistances on the client side? You might need to use RemoteEvents for this instance, try this:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = seat.Occupant
	local character = humanoid.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if seat.Occupant then
		Event:FireClient(player)
	end
end)
--Client side in a LocalScript
local Player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnClientEvent:Connect(function()
    if player.CameraMinZoomDistance == 0.5 then
        player.CameraMinZoomDistance = 15

    elseif player.CameraMinZoom.Distance == 15 then
        player.CameraMinZoomDistance = 0.5
    end
end)
1 Like

The script seems to be working at first, but when I exit the car the CameraMinZoom distance stays the same until I get back on the car again.
Here’s what it looks like:
https://gyazo.com/b509562a779cbd881d9ba024d0ac8475
I think a while true do loop would fix this but I am not too sure.

Try RunService Heartbeat instead as while true runs on every frame permanently

1 Like

Also I wouldn’t suggest that as it would permanently loop you shouldn’t use if statements in this, you should make 2 separate functions and call one when someone is seated then call the other one when the player isn’t seated

Try seeing if there is a weld instead of there being an occupant, as that is how roblox handles seats from my knowledge, via welding.

You could use:

seat:ChildAdded:Connect(function(weld)
       if weld then
           print(weld.Part1)
      end
end)

seat:ChildRemoving:Connect(function(weld)
       if weld then
           print(weld.Part1)
      end
end)

the

1 Like

I tried using this method and the ChildAdded function worked but the ChildRemoving didn’t work, so I tried to see what was wrong by using prints:

seat.ChildRemoving:Connect(function(weld)
	if weld then
		local humpart = weld.Part1
		print(humpart)
	end
end)

And it did not print anything! I am guessing that the weld is removed before the script can check the weld, is there a way to fix this??

Nevermind! :man_facepalming: I forgot to remove the “if weld then” & now it works! Thank you guys for the help!!!

Sorry about not responding sooner; I had to go at the time. I was going to comment about this, but I wasn’t able to finish lol.

1 Like