I’m working on a hubodometer that counts the amount of revolutions a wheel has made and transfers that info into a number value. Here’s how it looks running.
This script is supposed to wait for the CurrentAngle of the Hinge that houses the display to be in between a value of 0 to 180 to raise the value of the mileage. There is a “alreadylogged” value, which acts as a debounce for the script to not increase the value after it has done so once. When the hinge is between 0 and -180 degrees, the “alreadylogged” value is disabled, allowing the script to count up once more. The script, however, isn’t functioning, and doesn’t count up at all. Here’s the code
currentangle = script.Parent.HingeConstraint.CurrentAngle
RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
if currentangle < 180 and currentangle > 0 and alreadylogged == false then
script.Parent.Parent.Display.Mileage.Value = script.Parent.Parent.Display.Mileage.Value + .00284090909
alreadylogged = true
elseif currentangle < 0 and currentangle > -180 and alreadylogged == true then
alreadylogged = false
end
end)
it does not make sense if the current angle is -150 that is less then 0 but not more the -180 and
if lets say 100 is more then 0 so it never become false and networkowner will be a problem as well make sure to fix that as well and the currentangle has to also be updated each heart breat because you have
removed its pointer you can fix this by just putting it after heartbeat or setting currangle = script.Parent.HingeConstraint each time you use it just check currentangle.CurrentAngle that way you still have the pointer not only the value
It seems like the issue with this script is that the currentangle variable is only assigned once outside of the RunService.Heartbeat function, and it never gets updated within the function. As a result, the condition within the function if currentangle < 180 and currentangle > 0 and alreadylogged == false will only ever evaluate to true or false based on the initial value of currentangle and won’t change thereafter.
To fix this issue, you need to move the assignment of currentangle inside the RunService.Heartbeat function so that it gets updated every frame. You can also add a print statement to check the value of currentangle during runtime for debugging purposes. Here’s the updated code:
alreadylogged = false -- initialize the debounce variable
RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
currentangle = script.Parent.HingeConstraint.CurrentAngle
print("Current angle:", currentangle) -- for debugging purposes
if currentangle < 180 and currentangle > 0 and not alreadylogged then
script.Parent.Parent.Display.Mileage.Value = script.Parent.Parent.Display.Mileage.Value + 0.00284090909
alreadylogged = true
elseif currentangle < 0 and currentangle > -180 and alreadylogged then
alreadylogged = false
end
end)
With this modification, currentangle will be updated every frame, and the condition within the function will evaluate based on the updated value. The print statement can be removed after debugging is complete.