Pretty much what I want is for the wheels to be spinning when the car is moving and the wheels to stop spinning when the car stops moving. The problem has something to do with line 4 I think I just wrote the spinning part wrong but I cannot think on how to fix it. Heres the script
local function CarMoving(wheel)
while true do
print("spinning wheels")
wheel.Orientation = wheel.Orientation + Vector3.new(0.1, 0, 0)
if script.Parent.CarMoving.Value == false then
break
elseif script.Parent.CarMoving.Value == true then
wait(0.1)
end
end
end
for i, wheel in pairs(script.Parent:GetChildren()) do
if wheel:IsA("BasePart") then
if script.Parent.CarMoving.Value == true then
CarMoving(wheel)
end
end
end
Wheels spinning gets printed every time car is moving then stops when car isn’t moving.
No, you can’t use WeldConstraint because it doesn’t have C0. You need to replace it with a Weld object, which requires manually setting the C0 and C1 properties.
will this make the wheel spin so I wouldn’t need my script? The wheel has can collide off and so does the whole car would this affect anything? Also the car moves using a tween since it’s not a drivable car.
Well, the wheel needs to be unanchored. It will still require a script.
Alternatively, you could use a hinge constraint to spin the wheel (which is easier to set up), but exploiters could just delete it if the network owner is set to them.
local function CarMoving(wheel)
while true do
print("spinning wheels")
wheel.Weld.C0 = wheel.Weld.C0 * CFrame.Angles(0.1, 0, 0)
if script.Parent.CarMoving.Value == false then
break
elseif script.Parent.CarMoving.Value == true then
wait(0.1)
end
end
end
for i, wheel in pairs(script.Parent:GetChildren()) do
if wheel:IsA("BasePart") then
if wheel.Name == "Wheel" then
if script.Parent.CarMoving.Value == true then
CarMoving(wheel)
end
end
end
end
No errors and print statement is running but wheels are not spinning, I did everything you said anything I messed up?
For moving the wheels, you should use TweenService instead.
Use the Changed event for checking the BoolValue instead of a while loop.
local tableforstoringtweens = {} -- for storing the tweens
script.Parent.CarMoving.Changed:Connect(function(newValue) -- whenever the value changes
if newValue == true then
for _, wheel in pairs(script.Parent:GetChildren()) do
if wheel:IsA("BasePart") then
local tween = game:GetService("TweenService"):Create(wheel, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1), {Orientation = Vector3.new(360,0,0)}) --plays an endless tween
table.insert(tableforstoringtweens, tween) --inserts the tween into the table for later
tween:Play()
end
end
elseif newValue == false then
for i, tween in pairs(tableforstoringtweens) do --loops through the table to cancel the tweens
tween:Cancel()
table.remove(tableforstoringtweens, i) --so that we don't have extra useless tweens later on
end
end
end)
The wheels aren’t anchored, so just use a HingeConstraint as @SubtotalAnt8185 said. If the car is resting on them then the wheels will roll when the vehicle moves on the ground.
If the vehicle isn’t Physics based then set the HingeConstraints to Motor and set the AngularVelocity based on the vehicles actual speed. You might need to fine tune it so they visually spin at the correct rpms.