Car goes crazy when sitting down

Hi, I animated a car with moon animator and then I made a script that loads the animation. It works but I have annother script that lets the player stay on the car when players stay on it. Without that script the car would go without the player. But somehow my car goes crazy when I sit down on a seat. I tried to anchor it but then the animation doesnt work. Heres a example:

It must be the script because if I delete it, the car doesnt go crazy. It is a local script and its in the StarterCharacterScripts Folder. Heres the script that lets the player stay on the car:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local CheckStates = true
local LastPartCFrame
local LastPart
local Function
local Function2
function CFrameLogic(Raycast, CFramePart)
	--------------------------------------------------------------- MOVE PLAYER TO NEW POSITON FROM OLD POSITION
	if Raycast.Instance ~= LastPart then
		LastPart = Raycast.Instance
		LastPartCFrame = nil
	end
	local Train = Raycast.Instance
	if LastPartCFrame == nil then -- If no LastTrainCFrame exists, make one!
		LastPartCFrame = Train.CFrame -- This is updated later.
	end
	local TrainCF = Train.CFrame
	local Rel = TrainCF * LastPartCFrame:inverse()
	LastPartCFrame = Train.CFrame -- Updated here.
	CFramePart.CFrame = Rel * CFramePart.CFrame -- Set the player's CFrame
	--print("set")
end
Function = RunService.Heartbeat:Connect(function()
	--------------------------------------------------------------- CHECK PLATFORM BELOW
	-- Build a "RaycastParams" object
	local MovingObjects = workspace.MovingObjects --Make a folder and put all moving objects into it and call the folder "MovingObjects"
	local HumanoidRootPart = player.Character.HumanoidRootPart
	local humanoid = player.Character.Humanoid
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
	raycastParams.FilterDescendantsInstances = {MovingObjects}
	raycastParams.IgnoreWater = true

	-- Cast the ray
	local Hit = workspace:Raycast(HumanoidRootPart.CFrame.p, Vector3.new(0,-50,0), raycastParams) --Casts a ray facing down
	--------------------------------------------------------------------------------------------
	if Hit and Hit.Instance:IsDescendantOf(MovingObjects) then -- Check if the ray hits and if it hits an object inside of MovingObjects
		if CheckStates then -- check if the character is jumping
			if humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics or humanoid:GetState() == Enum.HumanoidStateType.Running or humanoid:GetState() == Enum.HumanoidStateType.Landed or humanoid:GetState() == Enum.HumanoidStateType.PlatformStanding or humanoid:GetState() == Enum.HumanoidStateType.Seated or humanoid:GetState() == Enum.HumanoidStateType.GettingUp or humanoid:GetState() == Enum.HumanoidStateType.None or humanoid:GetState() == Enum.HumanoidStateType.Physics or humanoid:GetState() == Enum.HumanoidStateType.Ragdoll or humanoid:GetState() == Enum.HumanoidStateType.Dead then
				CFrameLogic(Hit, HumanoidRootPart)
			else
				LastPartCFrame = nil -- Clear the value when the player gets off.
			end
		else
			CFrameLogic(Hit, HumanoidRootPart)
		end
	else
		LastPartCFrame = nil -- Clear the value when the player gets off.
	end
	Function2 = player.Character.Humanoid.Died:Connect(function()
		Function:Disconnect() -- Stop memory leaks
		Function2:Disconnect() -- Stop memory leaks
	end)
end)

does anyone know what I must change to fix this glitch?

1 Like

Try disabling the CFrame logic once seated down, it changes the CFrame of the character and when seated since it’s welded to the car then the car moves as well, consequently they move each other hence the glitch occurs.

3 Likes

I think that will solve the problem but how can I make the local function stop when a player sits down? I have scripted the part where it checks if the player is sitting or not.

with local function I mean Cframelogic

heres the script that I tried to make:

local seat = game.Workspace.MovingObjects.Vehicle.Body.Seats:GetChildren()

local function sitting()

DoNothing()

end

local function NotSitting()

CFrameLogic()

end

seat.ChildAdded:Connect(sitting)

seat.ChildRemoved:Connect(NotSitting)