My car's wheels are not working correctly (snapping to parts, getting stuck)

Hello everybody! I am making a game. But my car’s wheels will often literally detach from the car and become props, and my car itself will often have the wheels snap off. How can I fix this??!


local function ExplodeCar(Car)
	local Body = Car:WaitForChild("HumanoidRootPart")
	local Wheels = Car:WaitForChild("Wheels"):GetChildren()
	for i, obj in pairs(Wheels) do
		for e, obj2 in pairs(obj:GetChildren()) do
			obj2:Destroy()
		end
		obj.CanCollide = true
		obj.Anchored = false
		obj.AssemblyLinearVelocity = Vector3.new(math.random(-10, 10), math.random(1, 10), math.random(-3, 3))
	end
	Body.Anchored = false
	Body.CanCollide = true
	Body.AssemblyLinearVelocity = Vector3.new(math.random(-10, 10), math.random(1, 10), math.random(-3, 3))
end
local function SetupCar()
	script.Parent = script.Parent:WaitForChild("HumanoidRootPart")
	script.Parent.Parent.BodyGyro.Parent = script.Parent
	script.Parent.Anchored = true
	local function SetupWheel(Wheel:MeshPart)
		local Pos = Wheel.Position - script.Parent.Position
		local WOV = Wheel.Orientation
		if Wheel then
			Wheel.CanCollide = false
			game["Run Service"].PreSimulation:Connect(function()
				Wheel.Anchored = true
				Wheel.CanCollide = false
				local Params = RaycastParams.new()
				Params.RespectCanCollide = true
				Params.IgnoreWater = true
				Params.FilterType = Enum.RaycastFilterType.Exclude
				Params.FilterDescendantsInstances = {script.Parent}
				local ray = workspace:Raycast(Wheel.Position, Vector3.new(0, -1, 0) * 100000, Params)
				if ray and ray.Distance then
					if ray.Distance < 5 then
						Wheel.Position = script.Parent.Position + Pos + Vector3.new(0, 2 - ray.Distance, 0) - Vector3.new(script.Parent.AssemblyLinearVelocity.X / script.Parent.Parent.Speed.Value * script.Parent.Parent.Speed.Value / 7.5)
						Wheel.Orientation = WOV + Vector3.new(Wheel.Position.X * 35, 0, 0)
					else
						Wheel.Position = script.Parent.Position + Pos - Vector3.new(script.Parent.AssemblyLinearVelocity.X / script.Parent.Parent.Speed.Value * script.Parent.Parent.Speed.Value / 7.5)
						Wheel.Orientation = WOV + Vector3.new(Wheel.Position.X * 35, 0, 0)
					end
				end
			end)
		end
	end
	for i, obj in pairs(script.Parent.Parent.Wheels:GetChildren()) do
		SetupWheel(obj)
	end
	script.Parent.Anchored = false
	game["Run Service"].PostSimulation:Connect(function()
		script.Parent.Anchored = false
		script.Parent.CanCollide = false
		script.Parent:WaitForChild("Engine").PlaybackSpeed = (math.abs(script.Parent.AssemblyLinearVelocity.X) / script.Parent.Parent.Speed.Value * 1.5) + 1
		local ray = workspace:Raycast(script.Parent.Position, Vector3.new(0, -1, 0) * 100000)
		local rayf = workspace:Raycast(script.Parent.Position + Vector3.new(1, 0, 0), Vector3.new(0, -1, 0) * 100000)
		local rayb = workspace:Raycast(script.Parent.Position - Vector3.new(1, 0, 0), Vector3.new(0, -1, 0) * 100000)
		if ray and ray.Distance then
			if rayb and rayf and rayb.Distance and rayf.Distance then
				print("YES")
				if rayb.Distance >= rayf.Distance then
					if rayb.Distance > ray.Distance then
						script.Parent.Orientation -= Vector3.new(0.25, 0, 0)
					end
				else
					if rayf.Distance > ray.Distance then
						script.Parent.Orientation += Vector3.new(0.25, 0, 0)
					end
				end
			end
			script.Parent.AssemblyLinearVelocity = Vector3.new(script.Parent.AssemblyLinearVelocity.X, 4 * 5 - ray.Distance * 5, script.Parent.AssemblyLinearVelocity.Z)
		end
	end)
	game.ReplicatedStorage.SendMovement.OnServerEvent:Connect(function(Player, A, D, V)
		script.Parent.AssemblyLinearVelocity = Vector3.new((V) * script.Parent.Parent.Speed.Value, script.Parent.AssemblyLinearVelocity.Y, script.Parent.AssemblyLinearVelocity.Z)
	end)
	local BP = Instance.new("BodyPosition")
	BP.Position = script.Parent.Position
	BP.Parent = script.Parent
	BP.MaxForce = Vector3.new(0, 0, math.huge)
	BP.D = 1200
	BP.P = 50000000
end

local Car = script.Parent.CarToLoad.Value:Clone()
for i, obj in pairs(Car:GetChildren()) do
	obj.Parent = script.Parent
end
Car:Destroy()
SetupCar()

Car model:
Cars (serverstorage).rbxm (34.7 KB)
Car (workspace).rbxm (3.7 KB)

1 Like