Currently making it so when you sit in a driver seat the CameraMinZoomDistance is adjusted and when you exit the it returns to normal.
Currently I have this script and when you leave the seat it doesn’t return to normal.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local originalDistance = player.CameraMinZoomDistance
if character then
local humanoid = character:WaitForChild("Humanoid")
local seat = script.Parent.Parent.Parent
local function onSeated()
player.CameraMinZoomDistance = 25
end
local function onExit()
player.CameraMinZoomDistance = originalDistance
end
humanoid.Seated:Connect(onSeated)
seat.ChildRemoved:Connect(onExit)
end
I am not sure if you would have to use a custom camera for it or not. If you have any ideas on how to make this work, that would be appreciated.
Since the function connected to the event Humanoid.Seated can take an argument indicating whether the player is entering or exiting the state of sitting, we can use this argument to know which function to call.
Here’s how to fix what’s wrong with the code.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local originalDistance = player.CameraMinZoomDistance
if character then
local humanoid = character:WaitForChild("Humanoid")
local seat = script.Parent.Parent.Parent
local function onSeated()
player.CameraMinZoomDistance = 25
end
local function onExit()
player.CameraMinZoomDistance = originalDistance
end
humanoid.Seated:Connect(function(active)
if active then
onSeated()
else
onExit()
end
end)
end
The seat variable can be removed, as it is now an unused variable.
You might just try this: because nothing else can sit there so it will probably be a player anyways.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local originalDistance = player.CameraMinZoomDistance
local humanoid = character:WaitForChild("Humanoid")
local seat = script.Parent.Parent.Parent
local function onSeated()
player.CameraMinZoomDistance = 25
end
local function onExit()
player.CameraMinZoomDistance = originalDistance
end
humanoid.Seated:Connect(function(active)
if active then
onSeated()
else
onExit()
end
end)