Making loops faster than renderstepped

So what I’m trying to do is to make a really quick gun, but the max is 1bullet per renderstep. Is there any way to make it shoots out more bullets without changing the renderstep? Making it two bullets per renderstep isnt the choice.

Like in a minute a gun can only shoot 3600 bullets. Any how to increase the number?

5 Likes

Render stepped is every frame, you cant get any faster then that, just shoot numerous bullets per frame, also Heartbeat is recommended instead.

2 Likes

As CodeNil said, you can’t make it “faster” than RenderStep. But you can simulate it faster by launching several bullets per RenderStep (or Hearbeat which is more reliable) and alternate their velocity and start point. Ie. shoot three bullets per RenderStep where each bullet is offset from one another forward by X distance.

2 Likes

What type of gun fires faster than 3,600RPM?

I want one :eyes:

25 Likes

Fire the bullets in a separate thread recursively. That way you can just wait the rpm between shots instead of renderstepped

You should only be using RenderStepped for rendering, as it fires before any render updates happen. You should be using either Stepped or Heartbeat. They both run parallel to the render updates, but Stepped fires before the physics update and Heartbeat fires after.

Unless, of course, this has to do with GUIs.

5 Likes

The answer here isn’t faster loops, it’s physically positioning the bullets to represent a faster frequency. Anything faster than one frame is practically meaningless, so if you want to fire 2 bullets per frame, put the first one further ahead than the second one.

5 Likes
game:GetService('RunService').RenderStepped:connect(function()
	for i=1,math.ceil((rpm/60)/60) do
		spawn(shoot)
	end
end)

This is the closest way to get > 3,600 rpm in render stepped

2 Likes

You shouldn’t be using RenderStepped for something that isn’t graphical.
Otherwise, the gun would fire slower for people with a poor framerate.
Heartbeat is a consistent 60 fires per second.

Anyway, assuming that the gun fires at 10,000RPM or 167RPS, every Heartbeat you could have the gun chamber 2.8 rounds then fire off the math.floor of the number of chambered rounds.
So:
Frame 1: 2.8 rounds in chamber, fire 2
Frame 2: 3.6 rounds in chamber, fire 3
Frame 3: 3.4 rounds in chamber, fire 3
Frame 4: 3.2 rounds in chamber, fire 3
Frame 5: 3 rounds in chamber, fire 3
Frame 6: 2.8 rounds in chamber, fire 2
etc.

While it may sound like a bad solution to fire multiple shots at the same time, no one will notice when there’s 60 bursts a second.

1 Like

That’s exactly what my code above does :confused:

18 Likes

I am both impressed and terrified at once.

3 Likes

Finally the difference between Stepped and Heartbeat. Never thought I’d ever know.

2 Likes

Good god that’s horrific please never use that code

Never said it was good code,it’s the only way to do it.

2 Likes

uh, your code actually very useful. Thank you about that

no dont use it

You’re going to cause some serious issues with roblox if you try to execute instructions more than 60Hz a second

Especially since you’re spawning a new thread each time

1 Like

Why would you wrap it in a spawn? The Lua VM in Roblox is singlethreaded, so only one thread can run at a time anyway. Aside from that, spawn postpones execution until the next frame, so this line of code causes all sorts of strange effects you may not notice straight away. I would just do shoot() instead of spawn(shoot).

PS: Heartbeat or Stepped is better here instead of RenderStepped, probably.

4 Likes
-- this method is very laggy
-- only recommended as last resort

local number = 10 -- the more, the faster (and laggier)

while true do

-- code

if math.random(0,number)==number then task.wait() end
end