Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with making this automatic gun less buggy?

I need help with making this automatic gun less buggy(what I mean by buggy is it may fire faster for a second then back to normal).

mouse.Button1Down:connect(function()
	mouseDown = true
   if auto then
	while mouseDown do
		wait()
		if cooldown == false then
			cooldown = true
			fire()
			wait(fireRate)
			cooldown = false
		end
	end
else
	 if cooldown == false then
	fire()
	cooldown = true
	wait(fireRate)
	cooldown = false
	end
end
end)

Thanks!

1 answer

✓ Accepted answer

It feels buggy because you have a wait() before anything happens when you click, thus there is an unnecessary delay between clicking and the fire() function getting called. To make the gun feel more responsive this wait() should be placed after the if statement has happened.

while mouseDown do
    if cooldown == false then
        cooldown = true
        fire()
        wait(fireRate)
        cooldown = false
    end
    wait()
end

Now, more importantly, you definitely should be using RunService.Heartbeat:Wait() (https://developer.roblox.com/en-us/api-reference/class/RunService) because wait() is only able to delay the script 1/30th of a second while using what I put above allows you to have a delay of half that.

If the script you provided isn't a localscript, it needs to be to use the renderstepped wait. There is no way to keep the script a non-local and have a smaller delay.

5 comments

0

It's not firing slower.. It's firing faster, or it shoots 2 at once...

0

Good Guy Here Take Up-vote and Gold ^_^ ????

0

emoji didnt send idk y

0

Thanks, I didn't know that RunService existed and I'm grateful you showed it to me. I also fixed the buggyness by making it a repeat until loop.

0

oh nice, I apologize for a late response

This discussion is preserved as it was. New answers, comments, and votes are closed.