Hello, I’m currently trying to code a gun system and an issue I’ve encountered is handling rapid fire. I was just wondering how I should go about handling the user’s input when it comes to rapid-fire.
By “rapid-fire” do you mean 1 click = 1 bullet, or 1 click (and hold) = multiple bullets?
Additionally, could you be more specific in terms of the “issues” you are encountering? Can you post an example of how you are currently ‘handling’ user-input?
Yeah sure, apologies.
I meant, click & hold => Multiple bullets, the issue I’m encountering is that I’m not really sure where to begin with it. I’ve got the single bullet down using the Mouse.Button1Down event, however I’m not sure how I should work ‘rapid-fire’
I don’t have a lot of experience with making automatic weapons but my initial inclination is to do
something like:
Create functions that connect .Button1Down
and .Button1Up
to setting a boolean mouseOneDown
to either true or false - and then do something like:
while mouseOneDown do
fireBullet()
wait()
end
By using wait()
you can’t specify exactly long you want between each bullet. Try something like
local BULLET_WAIT = 1/10
while mouseDown do
fireBullet()
local t = tick()
while tick() - t < BULLET_WAIT do
RunService.Heartbeat:Wait()
end
end
That should give you more precision. I doubt that this is the best way to do it because each frame determines when the next will occur instead of calculating an exact time for each bullet. Maybe somebody with more gun systems expertise can suggest something.