HingeConstraint Issue: Wheel Spin (SOLVED)

So basically, I am trying to make a wheel that spins with HingeConstraint Motor type, but there’s a problem, shortly after the wheel starts spinning, it stops. What’s unusual is, the problem only exists if the AngularVelocity is greater than/equal to 3. If it’s less than 3, it doesn’t work. If it’s greater/equal to, it works fine, but it spins way too fast, which is not what I want.

An event called “Spinning” is constantly being fired by another script, the Fly script. If the parameter is true, it spins, if it’s false, the AngularVelocity is set to zero.

Wheel Script:

local RS = game:GetService("ReplicatedStorage")

local Events = RS:FindFirstChild("Events")
local wheel = script.Parent

Events.Spinning.Event:Connect(function(value)
	if value == true then
		wheel.HingeConstraint.AngularVelocity = 1
	elseif value == false then
		wheel.HingeConstraint.AngularVelocity = 0
	end
end)

Fly Script:

local RS = game:GetService("ReplicatedStorage")

local events = RS:FindFirstChild("Events")
local fly = events:FindFirstChild("Fly")
local spinning = events:FindFirstChild("Spinning")

local part = script.Parent
local rotationSpeed = 0

local isSpinning = false
local spinStartTime = 0
local spinDuration = 0

local maxRotationSpeed = 75
local acceleration = maxRotationSpeed / 1.25

local decelerationFactor = 9

function spin()
	local lastTime = spinStartTime
	while isSpinning do
		spinning:Fire(true) -- Event constantly being fired
		local currentTime = tick()
		local elapsedTime = currentTime - spinStartTime

		if elapsedTime < spinDuration then
			if rotationSpeed < maxRotationSpeed then
				rotationSpeed = rotationSpeed + acceleration * (currentTime - lastTime)
				if rotationSpeed > maxRotationSpeed then
					rotationSpeed = maxRotationSpeed
				end
			end
			part.CFrame = part.CFrame * CFrame.Angles(math.rad(rotationSpeed), 0, 0)

			part.RotVelocity = Vector3.new(math.rad(rotationSpeed), 0, 0)

			lastTime = currentTime
		else
			if rotationSpeed > 0 then
				rotationSpeed = rotationSpeed - (maxRotationSpeed / spinDuration) * decelerationFactor * (currentTime - lastTime)
				if rotationSpeed < 0 then
					rotationSpeed = 0
				end
			end
			part.CFrame = part.CFrame * CFrame.Angles(math.rad(rotationSpeed), 0, 0)

			part.RotVelocity = Vector3.new(math.rad(rotationSpeed), 0, 0)

			lastTime = currentTime

			if rotationSpeed == 0 then
				isSpinning = false
				spinning:Fire(false) -- Event is stopped, setting its value to false
			end
		end

		task.wait()
	end
end


fly.Event:Connect(function(duration)
	if not isSpinning then
		isSpinning = true
		spinStartTime = tick()
		spinDuration = duration
		spin()
	end
end)

Video of the issue (AngularVelocity: 1):

Try increasing the maxtorque of angularVelocity, also try set both wheel “Massless” property to true

You can adjust both scripts by these:

local RS = game:GetService("ReplicatedStorage")

local Events = RS:FindFirstChild("Events")
local wheel = script.Parent

Events.Spinning.Event:Connect(function(angularVelocity)
    wheel.HingeConstraint.AngularVelocity = angularVelocity
end)

Fly Script Update:

  • Modify the spin function to fire the Spinning event with a specific angular velocity instead of a boolean flag.
  • Adjust the angular velocity dynamically based on your game’s logic.

Here’s a simplified example of how you might approach this:

function spin()
	local lastTime = spinStartTime
	while isSpinning do
		local currentTime = tick()
		local elapsedTime = currentTime - spinStartTime

		if elapsedTime < spinDuration then
			if rotationSpeed < maxRotationSpeed then
				rotationSpeed = rotationSpeed + acceleration * (currentTime - lastTime)
				if rotationSpeed > maxRotationSpeed then
					rotationSpeed = maxRotationSpeed
				end
			end
		else
			if rotationSpeed > 0 then
				rotationSpeed = rotationSpeed - (maxRotationSpeed / spinDuration) * decelerationFactor * (currentTime - lastTime)
				if rotationSpeed < 0 then
					rotationSpeed = 0
				end
			end

			if rotationSpeed == 0 then
				isSpinning = false
			end
		end

        -- Convert rotationSpeed (degrees per second) to an appropriate angular velocity for the HingeConstraint
        local hingeAngularVelocity = math.clamp(rotationSpeed / 60, 0, 3) -- Example conversion, adjust as needed
        spinning:Fire(hingeAngularVelocity) -- Adjust the velocity directly

		lastTime = currentTime
		task.wait()
	end
    spinning:Fire(0) -- Stop spinning
end

Still didn’t fix the problem, it starts spinning then shortly stops.

By the way, the focus here is on the dark gray gear with 4 cylinders.

local RS, wheel, hinge, s, d, c, i = game:GetService("ReplicatedStorage"), script.Parent, script.Parent:FindFirstChildOfClass("HingeConstraint"), 50, 5, 10, false

local function spin(sp) if not i then i, hinge.AngularVelocity = true, sp or s; wait(d); hinge.AngularVelocity = 1, i = false; wait(c - d) end end
local function adjust(ns, nd, nc) s, d, c = ns or s, nd or d, nc >= d and nc or c end
local function specialSpin(m, du) if not i then i, hinge.AngularVelocity = true, s * (m or 1); wait(du or d); hinge.AngularVelocity = 1, i = false; wait((c > du and c or du) - d) end end
local function reset() s, d, c, i = 50, 5, 10, false; hinge.AngularVelocity = 0 end
local function lock(t) if not i then i, hinge.AngularVelocity = true, 0; wait(t); i = false end end
local function unlock(us, pud) if i then hinge.AngularVelocity = us or s; wait(pud or d); hinge.AngularVelocity = 1, i = false end end

RS.Events.Spin.Event:Connect(spin)
RS.Events.AdjustSettings.Event:Connect(adjust)
RS.Events.SpecialSpin.Event:Connect(specialSpin)
RS.Events.Reset.Event:Connect(reset)
RS.Events.Lock.Event:Connect(lock)
RS.Events.UnlockSpin.Event:Connect(unlock)

I still haven’t managed to fix this. I need solutions.

Ok what exactly is the part and the script that leads to it

Like mentioned before, the issue is on the gray part with 4 cylinders.

See the original message for the scripts.

The first script is for the part, the second script is for the fly.