Camera bugs on dismount

Hello, I’m a new programmer. Actually, I love Roblox Studio too much! But, now I have a problem… When you crash, your camera bugs. It follows a part and still following it 'till it destroys or the player is dead. I think it might be happening because of SetNetworkOwner. I would really appreciate if you guys help!

Here’s the footage (Sorry for potato mic!):

Da codes:

Damager (Script)

local event = script.Parent.Parent.Parent.Damager
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude

local isCooldown = false 
local cooldownTime = 1.5 

event.Event:Connect(function(damage, ragdollTime)
	if isCooldown then return end 
	isCooldown = true

	damage = damage * 6
	local parts = workspace:GetPartsInPart(script.Parent, overlapParams)
	local processedCharacters = {} 

	for _, part in pairs(parts) do
		local character = part.Parent
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		local rootPart = character:FindFirstChild("HumanoidRootPart")

		if humanoid and rootPart and not processedCharacters[character] then
			processedCharacters[character] = true
			humanoid:TakeDamage(damage)

			if damage > 20 then
				humanoid.PlatformStand = true 

				for _, v in pairs(character:GetDescendants()) do
					if v:IsA("BasePart") then
						v.CollisionGroup = "Ragdoll"
					elseif v:IsA("BallSocketConstraint") then
						v.Enabled = true
					elseif v:IsA("Motor6D") then
						v.Enabled = false
					end
				end

				task.delay(ragdollTime, function()
					if not character or not character.Parent or not rootPart then return end

					while rootPart and rootPart.AssemblyLinearVelocity.Magnitude > 1.5 do
						task.wait(0.2)
					end

					if humanoid then
						humanoid.PlatformStand = false
						humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
					end

					for _, v in pairs(character:GetDescendants()) do
						if v:IsA("BallSocketConstraint") then
							v.Enabled = false
						elseif v:IsA("Motor6D") then
							v.Enabled = true
						end
					end
				end)
			end
		end
	end

	task.wait(cooldownTime)
	isCooldown = false
end)

VehicleScript (Script)

local MAX_SPEED = 170
local MAX_STEER = 30
local acceleration = 3

task.wait(1.6)

local vehicle = script.Parent.Chassis
local driverSeat = vehicle.DriverSeat
local motorLeft, motorRight = vehicle.MotorLeft, vehicle.MotorRight
local servoLeft, servoRight = vehicle.ServoLeft, vehicle.ServoRight

local function updateNetworkOwner(player)
	for _, v in pairs(vehicle:GetDescendants()) do
		if v:IsA("BasePart") then
			v:SetNetworkOwner(player)
		end
	end
end

driverSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = driverSeat.Occupant
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		updateNetworkOwner(player)
	else
		updateNetworkOwner(nil)
	end
end)

driverSeat.Changed:Connect(function(property)
	if property == "ThrottleFloat" and script.Parent.Health.Value < 160 then
		local velocity = MAX_SPEED * acceleration * driverSeat.ThrottleFloat
		motorLeft.AngularVelocity = velocity
		motorRight.AngularVelocity = velocity
	elseif property == "SteerFloat" then
		local angle = MAX_STEER * driverSeat.SteerFloat
		servoLeft.TargetAngle = angle
		servoRight.TargetAngle = angle
	end
end)

Crash (Script, I don’t even know if you need this.)

local carparts = script.Parent:GetDescendants()
local crashvelo = 18
local cooldownTime = 0.5
local cooldowns = {}
local resparts = {}


for _, v in pairs(carparts) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit:IsA("BasePart") and not table.find(carparts, hit) then
				if cooldowns[v] and tick() - cooldowns[v] < cooldownTime or table.find(resparts, v) then
					
					return
				end
				local diffLX = math.abs(v.AssemblyLinearVelocity.X - hit.AssemblyLinearVelocity.X)
				local diffLY = math.abs(v.AssemblyLinearVelocity.Y - hit.AssemblyLinearVelocity.Y)
				local diffLZ = math.abs(v.AssemblyLinearVelocity.Z - hit.AssemblyLinearVelocity.Z)
				local diffAX = math.abs(v.AssemblyAngularVelocity.X - hit.AssemblyAngularVelocity.X)
				local diffAY = math.abs(v.AssemblyAngularVelocity.Y - hit.AssemblyAngularVelocity.Y)
				local diffAZ = math.abs(v.AssemblyAngularVelocity.Z - hit.AssemblyAngularVelocity.Z)
				local isLinearLow = diffLX < crashvelo or diffLY < crashvelo or diffLZ < crashvelo
				local isAngularLow = diffAX < crashvelo or diffAY < crashvelo or diffAZ < crashvelo
				
				if isLinearLow and isAngularLow then
					
					return
				else
					
					table.insert(resparts, v)
					cooldowns[v] = tick()
					local rad = Instance.new("Part")
					rad.Shape = Enum.PartType.Ball
					rad.Parent = v
					rad.Position = v.Position
					rad.Anchored = true
					rad.CanCollide = false
					rad.Transparency = 0
					Instance.new("Highlight", rad)
					script.Parent.Parent.Health.Value = script.Parent.Parent.Health.Value + 1
					
					local maxDiff = math.max(diffLX, diffLY, diffLZ)
					local calculatedSize = maxDiff / 100
					local damage = calculatedSize * 10
					rad.Size = Vector3.new(calculatedSize, calculatedSize, calculatedSize)

					local event = script.Parent.Parent.Damager
					
					event:Fire(damage, calculatedSize)
					local params = OverlapParams.new()
					params.FilterType = Enum.RaycastFilterType.Include
					params.FilterDescendantsInstances = carparts
					local partsInPart = workspace:GetPartsInPart(rad, params)
					
					for _, part in pairs(partsInPart) do
						if part:IsA("BasePart") then
							local parentModel = part:FindFirstAncestorOfClass("Model")
							local humanoid = parentModel and parentModel:FindFirstChildOfClass("Humanoid")
							if humanoid then
					
								local ragdollTime = parentModel:FindFirstChild("RagdollTime")
								humanoid:TakeDamage(damage)
								if ragdollTime then
									ragdollTime.Value = damage / 10
					
								end
								humanoid:ChangeState(Enum.HumanoidStateType.Physics)
							else
					
								cooldowns[part] = tick()
								part:BreakJoints()
							end
						end
					end
					rad:Destroy()
				end
			end
		end)
	end
end
1 Like

Heyo, pretty sure roblox has a built-in vehicle camera behavior that doesnt always restore the players normal camera settings automatically.

You can fix this by manually resetting the camera as soon as the player leaves the seat

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cam = workspace.CurrentCamera

local function resetCamera()
    local character = player.Character
    if character then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            cam.CameraType = Enum.CameraType.Custom
            cam.CameraSubject = humanoid
        end
    end
end

local function onSeatChanged(seat, occupant)
    if occupant ~= player.Character:FindFirstChildOfClass("Humanoid") then
        resetCamera()
    end
end

local vehicleSeat = workspace:WaitForChild("VehicleSeat")
vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    onSeatChanged(vehicleSeat, vehicleSeat.Occupant)
end)

Let me know if it works!

2 Likes

Hello! Thank you for your reply but… The problem still same, It didn’t worked :confused:
But hey, maybe it’s cuz I adapted it my carSpawner System.

Here, The codes:

CarSpawner (Script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player)
    local charName = "Car1" .. player.Name
    local existingCar = workspace:FindFirstChild(charName)
    
    if existingCar then
        existingCar:Destroy()
    end

    local car = ServerStorage:WaitForChild("Car1"):Clone()
    car.Name = charName
    car.Parent = workspace
    
    local spawnPart = player.Character and player.Character:FindFirstChild("Part")
    if spawnPart then
        car:MoveTo(spawnPart.Position)
    end
end)

CamRestorer (LocalScript)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cam = workspace.CurrentCamera

local function resetCamera()
    local character = player.Character
    if character then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            task.wait(0.1)
            cam.CameraType = Enum.CameraType.Custom
            cam.CameraSubject = humanoid
        end
    end
end


player.CharacterAdded:Connect(function(character)
    local humanoid = character:WaitForChild("Humanoid")
    

    humanoid.Seated:Connect(function(active, seat)
        if not active then
            resetCamera()
        end
    end)
end)

IM SO DUMB I FORGOT TO DO THE HEAD SUBJECT NOT HUMANOID!!!
@LepyyW thank you so much

1 Like

Haha! No worries, glad it fixed your issue! :slight_smile:

1 Like

Man I love you so much I’m tryin to fix ts for hours :heart: :love_letter:

2 Likes