You can write your topic however you want, but you need to answer these questions:
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.
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!!
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)
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.
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)