I have a first person module in a game I’m working on but whenever I sit in a vehicle seat (not a normal seat) it hides the character and never shows when you jump out of it. I want the character to be visible in both seats.
First Person Module:
local FirstPersonSettings = require(script:WaitForChild("FirstPersonSettings"))
local Bobbing = require(script:WaitForChild("Bobbing"))
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local contentProvider = game:GetService("ContentProvider")
local player = game.Players.LocalPlayer
local gui = player.PlayerGui:WaitForChild("FirstPerson")
local cursor = player.PlayerGui:WaitForChild("FirstPersonCursor")
local character
local humanoid
local currentCamera
local DEFAULT_FOV = 70
local DEFAULT_MINZOOM = player.CameraMinZoomDistance
local DEFAULT_MAXZOOM = player.CameraMaxZoomDistance
local root
local neck
local head
local yOffset
local CFNew, CFAng, asin = CFrame.new, CFrame.Angles, math.asin
local died = false
local HeadCam
local HeadCamWeld
local conns = {}
local RenderSteppedConn, CharDescendantAddedConn, CharAddedConn
local started = false
local enabled = false
local module = {}
function module:Enable()
enabled = true
initCharacterAddedVariables()
clearConns()
initGlobalConns()
initFirstPerson()
initCharacterAddedConn()
module:SetMouseEnabled(false)
Bobbing.Enable()
started = true
end
function module:Disable()
enabled = false
if not character
or not character:FindFirstChild("Humanoid")
then
return
end
started = false
clearConns()
if HeadCam then
HeadCam:Destroy()
HeadCam = nil
end
currentCamera.FieldOfView = DEFAULT_FOV
currentCamera.CameraSubject = character:WaitForChild("Humanoid")
player.CameraMinZoomDistance = DEFAULT_MINZOOM
player.CameraMaxZoomDistance = DEFAULT_MAXZOOM
for _, child in pairs(character:GetDescendants()) do
if child:IsA("Accessory") then
for _, accessory in pairs(child:GetChildren()) do
if accessory:IsA("MeshPart") then
accessory.Transparency = 0
end
end
elseif child.Name == "Head" and child:IsA("MeshPart") then
child.Transparency = 0
end
end
Bobbing.Disable()
module:SetMouseEnabled(true)
end
function module:SetMouseEnabled(state: boolean)
if typeof(state) ~= "boolean"
or not gui
or not gui:FindFirstChild("MouseLock")
or not cursor
then
return
end
userInputService.MouseIconEnabled = state
gui.MouseLock.Modal = state
cursor.Enabled = (not state)
end
function initCharacterAddedConn()
CharAddedConn = player.CharacterAdded:Connect(function(char)
if not enabled then
return
end
if started then
return
end
initCharacterAddedVariables()
clearConns()
initGlobalConns()
initFirstPerson()
Bobbing.Enable()
started = true
end)
end
function initFirstPerson()
currentCamera.FieldOfView = FirstPersonSettings.FIELD_OF_VIEW
currentCamera.CameraSubject = HeadCam
local newCframe = HeadCam.CFrame * CFrame.new(FirstPersonSettings.HEAD_CAM_OFFSET)
currentCamera.CFrame = newCframe
player.CameraMinZoomDistance = 0
player.CameraMaxZoomDistance = 0
if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
contentProvider:PreloadAsync(character:GetDescendants())
for _, child in pairs(character:GetDescendants()) do
if child:IsA("Accessory") then
for _, accessory in pairs(child:GetChildren()) do
if accessory:IsA("MeshPart") then
accessory.Transparency = 1
end
end
elseif child.Name == "Head" and child:IsA("MeshPart") then
child.Transparency = 1
end
end
CharDescendantAddedConn = character.DescendantAdded:Connect(function(child)
wait()
if child:IsA("Accessory") then
for _, accessory in pairs(child:GetChildren()) do
if accessory:IsA("MeshPart") then
accessory.Transparency = 1
end
end
end
end)
local changed_1 = false
local changed_2 = false
RenderSteppedConn = runService.RenderStepped:Connect(function()
if not died and not humanoid.SeatPart then
if not changed_1 then
changed_1 = true
changed_2 = false
Bobbing.Enable()
end
local x1, y1, z1 = currentCamera.CFrame:ToOrientation()
local x2, y2, z2 = character.PrimaryPart.CFrame:ToOrientation()
character.PrimaryPart.CFrame *= CFrame.Angles(0, y1 - y2, 0)
--if neck then
-- local camDirection = root.CFrame:toObjectSpace(currentCamera.CFrame).lookVector
-- local neckBefore = neck.C0
-- neck.C0 = CFNew(0, yOffset, 0) * CFAng(asin(camDirection.Y), -asin(camDirection.X), 0)
-- if neck.C0 ~= neckBefore then
-- game.ReplicatedStorage:WaitForChild("NeckEvent"):FireServer(neck.C0)
-- end
--end
elseif not died and humanoid.SeatPart and not changed_2 then
changed_2 = true
changed_1 = false
Bobbing.Disable()
end
end)
elseif character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
contentProvider:PreloadAsync(character:GetDescendants())
for _, child in pairs(character:GetDescendants()) do
if child:IsA("Accessory") then
for _, accessory in pairs(child:GetChildren()) do
if accessory:IsA("Part") or accessory:IsA("MeshPart") then
accessory.Transparency = 1
end
end
elseif child.Name == "Head" and child:IsA("Part") then
child.Transparency = 1
end
end
CharDescendantAddedConn = character.DescendantAdded:Connect(function(child)
wait()
if child:IsA("Accessory") then
for _, accessory in pairs(child:GetChildren()) do
if accessory:IsA("Part") or accessory:IsA("MeshPart") then
accessory.Transparency = 1
end
end
end
end)
character.PrimaryPart = root
head.Size = Vector3.new(1,1,1)
neck.Parent = head
neck.Part0 = character.Torso
neck.Part1 = character.Head
neck.DesiredAngle = 0
neck.MaxVelocity = 0
wait()
RenderSteppedConn = runService.RenderStepped:Connect(function()
if not died and not humanoid.SeatPart then
local x1, y1, z1 = currentCamera.CFrame:ToOrientation()
local x2, y2, z2 = character.PrimaryPart.CFrame:ToOrientation()
character.PrimaryPart.CFrame *= CFrame.Angles(0, y1 - y2, 0)
--if neck then
-- local camDirection = root.CFrame:toObjectSpace(currentCamera.CFrame).lookVector
-- local neckBefore = neck.C0
-- neck.C0 = CFNew(0, yOffset, 0) * CFAng(asin(camDirection.Y) - math.rad(90), 0, -asin(camDirection.X) + math.rad(180))
-- if neck.C0 ~= neckBefore then
-- game.ReplicatedStorage:WaitForChild("NeckEvent"):FireServer(neck.C0)
-- end
--end
end
end)
end
end
function initCharacterAddedVariables()
character = player.Character or player.CharacterAdded:Wait()
currentCamera = workspace.CurrentCamera
root = character:WaitForChild("HumanoidRootPart")
neck = character:FindFirstChild("Neck", true)
head = character:WaitForChild("Head")
humanoid = character:WaitForChild("Humanoid")
yOffset = neck.C0.Y
died = false
head.CanCollide = true
local old = character:FindFirstChild("HeadCameraPart", true)
if old then
old:Destroy()
end
HeadCam = Instance.new("Part")
HeadCam.Parent = head
HeadCam.Name = "HeadCameraPart"
HeadCam.Size = Vector3.new(0.5,0.5,0.5)
HeadCam.CFrame = head.CFrame * CFrame.new(FirstPersonSettings.HEAD_CAM_OFFSET)
HeadCam.Transparency = 1
HeadCam.CanCollide = false
HeadCamWeld = Instance.new("WeldConstraint")
HeadCamWeld.Parent = HeadCam
HeadCamWeld.Name = "HeadCameraPartWeld"
HeadCamWeld.Part0 = HeadCam
HeadCamWeld.Part1 = head
HeadCam.Parent = character
end
function clearConns()
for _, conn in ipairs(conns) do
conn:Disconnect()
conn = nil
end
if RenderSteppedConn then RenderSteppedConn:Disconnect() RenderSteppedConn = nil end
if CharDescendantAddedConn then CharDescendantAddedConn:Disconnect() CharDescendantAddedConn = nil end
if CharAddedConn then CharAddedConn:Disconnect() CharAddedConn = nil end
end
function initGlobalConns()
local InputBegan = userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
--if input.UserInputType == Enum.UserInputType.Keyboard then
-- if input.KeyCode == FirstPersonSettings.MOUSE_LOCK_OFF_KEY then
-- gui.MouseLock.Modal = true
-- end
--end
end)
table.insert(conns, InputBegan)
local InputEnded = userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
--if input.UserInputType == Enum.UserInputType.Keyboard then
-- if input.KeyCode == FirstPersonSettings.MOUSE_LOCK_OFF_KEY then
-- gui.MouseLock.Modal = false
-- end
--end
end)
table.insert(conns, InputEnded)
local Died = character.Humanoid.Died:Connect(function()
died = true
started = false
end)
table.insert(conns, Died)
local CharacterRemoving = player.CharacterRemoving:Connect(function()
HeadCam:Destroy()
end)
table.insert(conns, CharacterRemoving)
end
return module
Camera Bobbing Module:
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local Humanoid
local bobbing, sensitivity
local func1 = 0
local func2 = 0
local func3 = 0
local func4 = 0
local val = 0
local val2 = 0
local int = 10
local int2 = 10
local sensitivityMult = 0.4
local vect3 = Vector3.new()
local enabled = false
local module = {}
local number = 0.1 -- change this number to change speed
function module.Enable()
if bobbing then
bobbing:Disconnect()
bobbing = nil
end
if sensitivity then
sensitivity:Disconnect()
sensitivity = nil
end
initCharacterVariables()
enabled = true
bobbing = game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
deltaTime = deltaTime * 60
if Humanoid.Health <= 0 or not enabled then
bobbing:Disconnect()
enabled = false
return
end
local rootMagnitude = Humanoid.RootPart and Vector3.new(Humanoid.RootPart.Velocity.X, 0, Humanoid.RootPart.Velocity.Z).Magnitude or 0
local calcRootMagnitude = math.min(rootMagnitude, 50)
if deltaTime > 3 then
func1 = 0
func2 = 0
else
func1 = lerp(func1, math.cos(tick() * 0.5 * math.random(10, 15)) * (math.random(5, 20) / 1000) * deltaTime, 0.05 * deltaTime)
func2 = lerp(func2, math.cos(tick() * 0.5 * math.random(5, 10)) * (math.random(2, 10) / 1000) * deltaTime, 0.05 * deltaTime)
end
Camera.CFrame = Camera.CFrame * (CFrame.fromEulerAnglesXYZ(0, 0, math.rad(func3)) * CFrame.fromEulerAnglesXYZ(math.rad(func4 * deltaTime), math.rad(val * deltaTime), val2) * CFrame.Angles(0, 0, math.rad(func4 * deltaTime * (calcRootMagnitude / 5))) * CFrame.fromEulerAnglesXYZ(math.rad(func1), math.rad(func2), math.rad(func2 * 10)))
val2 = math.clamp(lerp(val2, -Camera.CFrame:VectorToObjectSpace((Humanoid.RootPart and Humanoid.RootPart.Velocity or Vector3.new()) / math.max(Humanoid.WalkSpeed, 0.01)).X * 0.08, 0.1 * deltaTime), -0.35, 0.2)
func3 = lerp(func3, math.clamp(UserInputService:GetMouseDelta().X * 0.5, -5, 5), 0.25 * deltaTime)
func4 = lerp(func4, math.sin(tick() * int) / 5 * math.min(1, int2 / 100), 0.25 * deltaTime)
if rootMagnitude > 1 then
val = lerp(val, math.cos(tick() * 0.5 * math.floor(int)) * (int / 200), 0.25 * deltaTime)
else
val = lerp(val, 0, 0.05 * deltaTime)
end
if rootMagnitude > 12 then
int = 20
int2 = 18
elseif rootMagnitude > 0.1 then
int = 12
int2 = 14
else
int2 = 0
end
vect3 = lerp(vect3, Camera.CFrame.LookVector, 0.125 * deltaTime)
end)
local mouseDeltaSensitivity = sensitivityMult / UserGameSettings.MouseSensitivity
UserInputService.MouseDeltaSensitivity = mouseDeltaSensitivity
sensitivity = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
mouseDeltaSensitivity = sensitivityMult / UserGameSettings.MouseSensitivity
UserInputService.MouseDeltaSensitivity = mouseDeltaSensitivity
end)
end
function module.Disable()
enabled = false
UserInputService.MouseDeltaSensitivity = UserGameSettings.MouseSensitivity
end
function initCharacterVariables()
Humanoid = Player.Character:WaitForChild("Humanoid")
end
function lerp(a, b, c)
return a + (b - a) * c
end
return module
Any help is appreciated since I don’t know if this is a roblox issue or an issue with my module ![]()
