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

Help with a shotgun bullet formula?

Hey, I'm making a gun kit and I need to make a shotgun function. How this will work is it will do a for loop and loop the fire() function. So, it looks like this.

for count = 0, shotgunbulletCount do
	fire()
end

However, because the fire() function subtracts one bullet, it will end up subtracting the value of shotgunbulletCount from the total ammo. This results in the gun running it way too quickly. Because of this, I've decided to try and subtract 1/shotgunbulletCount. What I did is I subtracted the fraction and because the for loop will loop the fire function, it should all subtract a total of 1 bullet. However, this didn't work. Please haaaalp!!!11!111!111

if not shotgun then
		currentAmmo = currentAmmo - 1
	else
		print(currentAmmo - 1/shotgunbulletCount)
		currentAmmo = currentAmmo - 1/shotgunbulletCount
end

btw it is subtracting .125..also this shotgun function is generally broken, so i will probably post more questions about it ;-;

3 comments

0

sorry if the explanation is bad, just ask if you have a question

0

The for loop will naturally iterate fire() the # of shotgunbulletCount time(s)

0

But the each fire() function removes 1 ammo. I need it to remove a fraction of it, so after the for loop it's subtracted 1 bullet.

1 answer

The way I handle it is to subtract the ammo before I run a loop.

function fire()
	ammo = ammo - 1
	for i = 0, bullet_amount do
		--insert raycasting and stuff here
	end
end

alternatively

ammo = ammo - 1
for i = 0, bullet_amount do
	fire()
end

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