[SOLVED] When leaving a vehicle, the game just breaks

I’m using raycasting to make the player’s character be in a “safe” position when leaving the vehicle. The issue with this is that, the script for doing it breaks my game basically

I have already identified that this is the script causing problems (Server Script)

local Players = game:GetService("Players")
local seat = script.Parent
local car = seat.Parent
local LeaveAttach = seat:WaitForChild("ExitPos")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		local player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		seat:SetAttribute("Current_Occupant", player.UserId)
	else
		local leavingOccupant = Players:GetPlayerByUserId(seat:GetAttribute("Current_Occupant"))
		local character = leavingOccupant.Character
		
		local RayParams = RaycastParams.new()
		RayParams.FilterDescendantsInstances = {car, character}
		RayParams.FilterType = Enum.RaycastFilterType.Exclude
		
		local tpdistance = -8
		local ray = workspace:Raycast(character.PrimaryPart.Position, seat.CFrame.LookVector * tpdistance, RayParams)
		local pos = seat.Position + seat.CFrame.RightVector * (ray and ray.Distance or tpdistance)
		character.PrimaryPart.Position = pos
		seat:SetAttribute("Current_Occupant", nil)
	end
end)

Here’s the script that just stops working when I enter and then leave the vehicle

local WeldConstraint = R15:FindFirstChildOfClass("WeldConstraint")

WeldConstraint:GetPropertyChangedSignal("Enabled"):Connect(function()
	local DustEmitter = ExplosionAttach:WaitForChild("Dust")
	local FlashEmitter = ExplosionAttach:WaitForChild("Flash")
	local SmokeEmitter = ExplosionAttach:WaitForChild("Smoke")
	local ExplosionLight = ExplosionEmit:WaitForChild("ExplosionLight")
	
	if R15:HasTag("HasAlreadyExploded") then
		for _,fireworkchild in pairs(R15:GetChildren()) do
			if fireworkchild:IsA("BasePart") or fireworkchild:IsA("Part") then
				fireworkchild.Anchored = true
			end
		end
	else
		ProximityPrompt:Destroy()
		R15:AddTag("HasAlreadyExploded")
		
              -- There's more code but it's unrelated
	end
end)

This video shows what happens:

Instead of directly modifying PrimaryPart.Position, you could try using a Humanoid:MoveTo()

character:MoveTo(pos)

It solved the part of the fireworks not having a chained explosion, but the character just doesn’t move nor get teleported at all

I also tried this:

character:SetPrimaryPartCFrame(CFrame.new(pos))

But it had the same result

My apologies, I found a fix after tweaking some things. Thanks for the help tho :wink:

task.wait()
character.PrimaryPart.CFrame = CFrame.new(pos.X, pos.Y, pos.Z)