so i have some abilities in my game that increase the walkspeed above average, and then a speed manager system that reduces the speed to the average amount overtime.
however, the manager is really fast, and the reduction happens almost instantly, so u don't get to use the extra speed at all.
i believe its because runservice is too fast.
im assuming i just have to do it without runservice, but the only thing other than runservice i can think of is a while loop, which are notoriously laggy.
so if u guys think of any other method, let me know.
#speed system is not working
1 messages · Page 1 of 1 (latest)
speed system is not working
runservice is not "too fast". it runs at a fairly stable 60fps most of the time unless there's something dropping framerate. while loops are not laggy either.
you're missing a simple little trick here
you see that deltaTime variable?
yea
so, that's a number that is how many seconds since the previous frame, as a fraction
usually 1/60 but framerates are never exact
hence the variable to say exactly how long it was
one simple little trick you can do to smooth things that happen in a heartbeat/renderstepped/etc when you have that deltatime variable, is just multiply anything with it to get a "per second" rate
for example
local counter=0
local smoothCounter=0
runservice.Heartbeat:Connect(function(deltaTime)
counter += 2 -- increases by 2 every frame (60fps)
smoothCounter += 2 * deltaTime -- increases at a rate of 2 per second (ALMOST irrespective of framerate)
end)
-- after 10 seconds, counter will have a value of 1200 while smoothCounter will have a value of 20.```
or in other words, if you want the walkspeed to decrease by some amount per second, just multiply the amount by the deltatime
e.g lossPerSecond = speed_reduce_rate * deltaTime
this also helps a lot with unstable framerate since deltatime over 1 second will (almost) always add up to 1
hope that helps. this trick is very useful and used all the time in heartbeat/renderstepped etc
you can do the same thing to task.wait with a while loop
while loops are kinda laggy tho arent they
while true do
local deltaTime=task.wait()
lossPerSecond=speed_reduce_wait * deltaTime
end``` since task.wait() is not precise either, and it returns the number of seconds *actually* waited as a fraction. if the requested wait time is 0 or not specified, it runs at the same rate as the other stepper functions like heartbeat.
no
the only thing that makes this kind of loop laggy, or heartbeat laggy for that matter, is doing too much work in the loop
e.g if you iterate 5 million instances every frame, that may cause lag, just because you're doing too much in a single frame that the work isn't finished by the time the frame needs to be rendered, so the game starts to lag and frames take longer to finish
similar can also happen for example if roblox loses focus it may start rendering much slower to save resources for other programs that have focus
np, you're going to use that a lot now that you know about it 🙂
it's almost as ubiquitous as a debounce pattern
would using a playerGUI thing to display the speed on screen every time cacuse lag
cause*
cause lag? no
displaying values that change every frame on the HUD via a screengui is a very common thing to do, it doesn't lag
why do you think everything lags? 😂
okay well, protip, you should not be caring about performance and optimization nearly as much as you are
ok
and anyone who tells you "do X for performance" is either making shit up, or is wasting your time
working is far more important than optimized, particularly for beginners
i recommend watching this
When should you optimize your code?
Access to code examples, deleted scenes, song names and more at https://www.patreon.com/codeaesthetic