#speed system is not working

1 messages · Page 1 of 1 (latest)

little basin
#

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.

little basin
#

speed system is not working

wise fern
#

you're missing a simple little trick here

#

you see that deltaTime variable?

little basin
#

yea

wise fern
#

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

wise fern
# little basin yea

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

little basin
#

while loops are kinda laggy tho arent they

wise fern
#
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.
wise fern
#

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

little basin
#

oooh

#

that makes sense

wise fern
#

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

little basin
#

also that smoothcounter method worked for me

#

thanks for teaching me that

wise fern
#

it's almost as ubiquitous as a debounce pattern

little basin
#

would using a playerGUI thing to display the speed on screen every time cacuse lag

#

cause*

wise fern
#

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? 😂

little basin
#

idk

#

im a little bit new

wise fern
little basin
#

ok

wise fern
#

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