Hello I’m making a GUI controller that uses runservice to fill up a circle, but the more fps I use the faster it fills up regarding the speed I give it. How can I make it more consistent?
While Loops just makes the circle go even slower even without the delay, so I cant use that.
local speed = 155 -- Amount of time until the circle reaches its destination.
if charging then
SteppedBlaster = run.Heartbeat:Connect(function(deltaTime: number)
local NormalSpeed = speed * deltaTime
Rotation += math.floor(NormalSpeed) -- Fills up the Circle.
end)
else
Rotation = 0 -- Resets the circle back to the beginning
SteppedBlaster:Disconnect()
end
Since you are using delta time (and you seem to be doing so correctly), it should increment the rotation independently from the FPS. Due to this, I suspect the real reason you’re experiencing issues with the circle filling up faster than it should, is because you aren’t currently checking to see if the heartbeat loop is already running before starting it, and since you seem to be creating the connection within a function that’s connected to another event (due to the end)), you run the risk of having multiple loops incrementing the rotation at once. To fix this problem, you’ll need to replace if charging then with if charging and not SteppedBlaster then, and make sure to set SteppedBlaster to nil after you disconnect it
I forgot to add to the script that the Charging also has a attribute changed signal so it’s not checking every second, it’ll disconnect when the gun isn’t charging anymore. But I want it to stay at a constant 60 despite the user’s preference while also adding it up.
local RunService = game:GetService("RunService")
local incrementSpeed = 60 -- Will increment the value by 60 each second
local maxValue = 360 -- The maximum value that value can reach
local value = 0
local function onHeartbeat(delta: number)
value = math.min(value + (incrementSpeed * delta), maxValue) -- math.min returns whichever value is the smallest
print(value)
end
RunService.Heartbeat:Connect(onHeartbeat)
Each second, value will be incremented by incrementSpeed in a gradual way, independently of the player’s FPS, since it’s multiplying the increment value by the delta time