Make a car move with a conveyor

So I am trying to move a car by making a conveyor system when the part is being touched, it will move the car but my code right now moves the car when it is off the ground.

local model = script.Parent
local WheelParts = model:WaitForChild("WheelParts")

local speed = 50

game:GetService("RunService").Heartbeat:Connect(function()
	for i,v in ipairs(WheelParts:GetChildren()) do
		if v:IsA("BasePart") then
			v.AssemblyLinearVelocity = Vector3.new(speed, 0, 0)
		end
	end
end)

1 Like

Add a .Touched function.

Code:

local model = script.Parent
local WheelParts = model:WaitForChild("WheelParts")

local speed = Vector3.new(50, 0, 0)

for i, v in WheelParts:GetChildren() do
	if v:IsA("BasePart") then
		v.Touched:Connect(function()
			v.AssemblyLinearVelocity = speed
		end)
	end
end

You could also use Raycasting if the above doesn’t work.

Code:

local RunService = game:GetService("RunService")

local model = script.Parent
local WheelParts = model:WaitForChild("WheelParts")

local speed = Vector3.new(50, 0, 0)

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {model}

RunService.Heartbeat:Connect(function()
	for i,v in WheelParts:GetChildren() do
		if v:IsA("BasePart") then
			local raycastResult = workspace:Raycast(v, Vector3.new(0, -10, 0), rayParams)
			
			if raycastResult then
				v.AssemblyLinearVelocity = speed
			end
		end
	end
end)
2 Likes

Thank you! I did a bit of modification to it and it seems to work perfectly:

local RunService = game:GetService("RunService")

local model = script.Parent
local WheelParts = model:WaitForChild("WheelParts")

local speed = 50

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {model}

RunService.Heartbeat:Connect(function()
	for i,v in ipairs(WheelParts:GetChildren()) do
		if v:IsA("BasePart") then
			local rayOrigin = v.Position
			local rayDirection = v.CFrame.UpVector * -1
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

			if raycastResult then
				v.AssemblyLinearVelocity = v.CFrame.LookVector * speed
			end
		end
	end
end)
1 Like

No problem. For better performance, get rid of the ipairs like I did. Roblox added generalized iteration, which performs faster than ipairs/pairs.

Code:

local RunService = game:GetService("RunService")

local model = script.Parent
local WheelParts = model:WaitForChild("WheelParts")

local speed = 50

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {model}

RunService.Heartbeat:Connect(function()
	for i,v in WheelParts:GetChildren() do
		if v:IsA("BasePart") then
			local rayOrigin = v.Position
			local rayDirection = v.CFrame.UpVector * -1
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

			if raycastResult then
				v.AssemblyLinearVelocity = v.CFrame.LookVector * speed
			end
		end
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.