How would i change a semi gun into an automatic?

Hello, so im trying to make a semi gun, into an automatic. but i literally got no idea how, here is my script that is in the gun

--Object and Sound Variables

local gun = script.Parent

local gun_sound = game.ReplicatedStorage["Gun shot"]

local empty_sound = game.ReplicatedStorage.clip_empty

local reload_sound = game.ReplicatedStorage.Reload

local player = game.Players.LocalPlayer

local clipSize = gun.Ammo.Value

local ammo = gun.Ammo

local shooting = false

local equipped = false



--UserInputService Setup

local userInput = game:GetService('UserInputService')



--Mouse Icon

local mouse = game.Players.LocalPlayer:GetMouse()



--Remote Event Setup



local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')



--Checks if Mouse is clicked



gun.Equipped:Connect(function(mouse)



	player.PlayerGui.ScreenGui.Ammo.Visible = true

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)



	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then

				remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)

				gun_sound:Play()
gun.Ammo.Value -= 1
		else

			empty_sound:Play()

		end

	end

)

	mouse.Button2Down:Connect(function()



		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 40



	end)



	mouse.Button2Up:Connect(function()



		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 70



	end)



end)



-- Unequip gun



gun.Unequipped:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Visible = false

end)





--Checks if the letter R is pressed to reload



userInput.InputBegan:Connect(function(input, gameProcessed)



	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.R then

				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then

					reload_sound:Play()

					reload_sound.Ended:Wait()

					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then

						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)

						gun.Ammo.Value = clipSize

					else

						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value

						gun.MaxAmmo.Value = 0

						player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)

					end



				end

			end

		end

	end

end)



-- Update ammo GUI



ammo.Changed:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
end)

Could anyone help me make it automatic?

2 Likes

Oh its quite simple. Just do:

local tool = -- tool
local Firing = false
tool.Activated:Connect(function()
	Firing = true
	while Firing do
		-- OH BABY A TRIPLE KILL!
		task.wait(60/RoundsPerMinute)
	end
end)
tool.Deactivated:Connect(function()
	Firing = false
end)

i tried doing the “while shooting do” thingy but then i can’t use the “else” statement. Do you know why?
pic

This happens because the “else” statement is inside the loop. Put an “end” before the else statement to fix it.

1 Like

like this?
image

1 Like
while shooting do
removeEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)

gun_sound:Play()
gun.Ammo.Value -= 1
end -- here
else

okay, ill try that. i hope it works!

while shooting do
	if gun.Ammo.Value > 0 then
		gun.Ammo.Value -= 1
	else
		-- empty play
	end
	task.wait()
end

It seems like your a newbie to developing. Let me know if you have issues understanding. I can guide you.

it made my whole roblox studio almost crash, also the ammo went from 33/33 to -11k in a second, i will never fix that bruh.

oooooohhhh, yeah i forgot about the wait statement, no wonder it crashed.

okay ima try this


			gun_sound:Play()
				gun.Ammo.Value -= 1
				end
		else

			empty_sound:Play()

		end
task.wait(0.1)
		end)

wow, still crashes, i swear to god, how do people make the switches (automatic pistols)

Are you putting the “task.wait(0.1)” inside the loop?

i did the thing crypt told me to do

while shooting do
	if gun.Ammo.Value > 0 then
		gun.Ammo.Value -= 1
	else
		-- empty play
	end
	task.wait()
end

Try adjusting the duration, see if that will fix it.


		end
task.wait(0.5)
		end)

it still crashes, its probablly about the task.wait placement or the whole script.

I have a feeling this is because of the fired server, try this to see if it outputs a warning

local gun = script.Parent

local gun_sound = game.ReplicatedStorage["Gun shot"]

local empty_sound = game.ReplicatedStorage.clip_empty

local reload_sound = game.ReplicatedStorage.Reload

local player = game.Players.LocalPlayer

local clipSize = gun.Ammo.Value

local ammo = gun.Ammo

local shooting = false

local equipped = false



--UserInputService Setup

local userInput = game:GetService('UserInputService')



--Mouse Icon

local mouse = game.Players.LocalPlayer:GetMouse()



--Remote Event Setup



local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')



--Checks if Mouse is clicked



gun.Equipped:Connect(function(mouse)



	player.PlayerGui.ScreenGui.Ammo.Visible = true

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)



	mouse.Button1Down:Connect(function()
               while shooting do
		if gun.Ammo.Value > 0 then

                          local success, err = pcall(function()

				remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)

				gun_sound:Play()
gun.Ammo.Value -= 1
end)
if success then
print("fired")
else
warn(err)
end
		else

			empty_sound:Play()

		end
         task.wait(0.5)
end

	end

)

	mouse.Button2Down:Connect(function()



		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 40



	end)



	mouse.Button2Up:Connect(function()



		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 70



	end)



end)



-- Unequip gun



gun.Unequipped:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Visible = false

end)





--Checks if the letter R is pressed to reload



userInput.InputBegan:Connect(function(input, gameProcessed)



	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.R then

				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then

					reload_sound:Play()

					reload_sound.Ended:Wait()

					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then

						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)

						gun.Ammo.Value = clipSize

					else

						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value

						gun.MaxAmmo.Value = 0

						player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)

					end



				end

			end

		end

	end

end)



-- Update ammo GUI



ammo.Changed:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
end)

it doesn’t output anything, the gun doesn’t even shoot anymore.

Assuming the gun model you used was the “realistic gun” on your profile, I put it in roblox studio and recoded it. Is this what you anticipated?

wrong video sorry, it’s fixed now

robloxapp-20240106-1832412.wmv (2.5 MB)