.heartbeat does not disconnect

runservice heartbeat doesnt seem to disconnect when told

function discon()
	if torqu then
		torqu:Disconnect()
		torqu = nil
		warn(303)
	end
	warn(404)
	t1.Torque = Vector3.new(0, 0, 0)
	t2.Torque = Vector3.new(0, 0, 0)
	t3.Torque = Vector3.new(0, 0, 0)
end

function setbrake(percent: number)
	local udir = w2.CFrame:VectorToObjectSpace(w2.AssemblyAngularVelocity)
	local dir = 0
	if math.round(udir.X) ~= 0 then
		dir = udir.X / math.abs(udir.X)

	elseif math.round(udir.X) == 0 or percent == 0 then
		dir = 0
		
		discon() -- it think it failed to disconnect here
		
		for i, part in ipairs(engine:GetDescendants()) do
			if part:IsA("BasePart") then
				part.AssemblyAngularVelocity = Vector3.new(0,0,0)
				part.AssemblyLinearVelocity = Vector3.new(0,0,0)
			end
		end
		return
	end

	t1.Torque = Vector3.new(-percent * dir * bpower, 0, 0)
	t2.Torque = Vector3.new(-percent * dir * bpower, 0, 0)
	t3.Torque = Vector3.new(-percent * dir * bpower, 0, 0)
end

b.OnServerEvent:Connect(function(player, percentage)
	brk.TargetAngle = 180 * percentage
	print(percentage)
	if percentage > 0 then
		torqu = render.Heartbeat:Connect(function(delta)
			setbrake(percentage)
		end)
		return
	elseif percentage == 0 then
		discon()
		return
	end
end)

script output gets spammed
yeah

any kind of help is appreciated!

2 Likes
if percentage > 0 then
        if torqu then return end --prevents stacking connections
		torqu = render.Heartbeat:Connect(function(delta)
			setbrake(percentage)
		end)
		return
1 Like

a really weird issue that pops up when the vehicle is moving and i’m applying the brakes


i dont really understand how the same value can change like this
its fine when the vehicle is idle

1 Like

there is no such thing as -0, use <= 0 instead

1 Like

print is inaccurate at these speeds

1 Like

in lua there is no such thing like Double.NegativeZero in other languages,
instead you should use the less than or equal to operator <=

1 Like

thats a typo its percentage == 0 in the script

1 Like

im confused, what do you mean speed?
did you mean the vehicle speed? if so it wasnt even going that fast.
or
did you mean how fast heartbeat is? if so that cant be the case since it workes perfectly fine when the vehicle is idle
the value that changes is percentage a number that should not change between 1 and 3 (see image)

1 Like

i mean when you print that quickly to the output it can be incorrect because print isn’t as fast as heartbeat, it can produce weird results

also, you can set network ownership to clients and allow them to drive their own car, aka you can do this math on the client to save the headache of using remote events except for like things like brake lights or such

also, reusing the same torque event for all clients who use a car is a bad idea, it means multiple people will be braking each other. ergo your function structure is incorrect

1 Like

???
first, brake power changes when running and brake power depends on percent i.e percent is actually different when running not just print being weird

second, it just 1 vehicle here not 2 or more, 1 server server script for each vehicle, you could go on and say thats bad or whatever but im asking why percent is different when running not “is my script bad/inefficient?”

1 Like

what do you think this does?

local t

t = game:GetService("RunService").Heartbeat:Connect(function()
	print("1")
end)

t = game:GetService("RunService").Heartbeat:Connect(function()
	print("2")
end)

if you said “it disconnects”, you’re wrong. t does not disconnect just because its reassigned, its instead left the connection without a variable.
image

this is occurring every new update you’re sending in the remote event, and when percentage is 0, you’re only disconnecting the last connection torqu was assigned. you have to disconnect torqu every time before reassigning to it. you shouldn’t even have to be doing this though, thus why I said your function structure is incorrect.

t:Disconnect()

you can disconnect heartbeat, its in the script

you’re not listening, youre only disconnecting on 0, not anything between 1 and 100 percent. in fact you’re making a brand new connection every update that isn’t 0, which is a memory leak, and is not capable of being disconnected. that’s why it isn’t disconnecting

t:Disconnect() will not disconnect the .Heartbeat event that prints “1” in the example.