How do i put reload to a gun

Well first of all i tried to put reload in to a semi-automatic gun. It works!

But here are the issues…

First when the gun finishes the last bullet the reload isn’t done instantly, instead you have to click again for the gun to reload, and when it does reload well everything goes according to plan except that once the reloading is complete the gun actually shoots even if you didn’t click the mouse. It does make sense due to how i made the script(Local script) But i just couldn’t fix this, so in conclusion the gun does reload but i want the gun to reload instantly once the gun runs out of bullets, and second how the hell do i fix the gun shooting itself once done reloading

Here is the local script:

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastP = RaycastParams.new()
raycastP.FilterDescendantsInstances = {player.Character}
local ammo = 6
local max_ammo = 6
local reloading = false

local function reload()
	tool.Handle.Reload:Play()
	reloading = true
	task.wait(1)
	ammo = max_ammo
	reloading  = false
	task.wait(1)
end

tool.Activated:Connect(function()
	if shooting == false and reloading == false then
		if ammo == 0 then
			reload()
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastP)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")
			
			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(2)
		shooting = false
	end
end)
1 Like

If ya know how i can make the code a little better then let me know too!
Im not so good at organizing code lol

mouse.KeyDown:Connect(function(key)
	if key == "r" then
		if reloading == false then
			reload()
		end
	end
end)

This code is easy explained if reloading is false then the reloading will set to be true and it will reload your gun.

I will add that later, Thx for the advice tho

But seriously someone help me fix the issues that i have with my God Damn freaking gun

you check the ammo amount before you fire the bullet so it won’t happen after you have fired, in that case you have to put this line at the end of the fire function. Next up you don’t actually stop the function after reloading so it will make an odd thing where you automatically fire after the reload is done, you can add return after reload to stop the function

Maybe try this code, this code is an easy fix to make the gun reload with 1 ammo or more left.

mouse.Button1Down:Connect(function()
	if ammo == 0 then
		reload()
	end
	ammo -= 1
	tool.Handle.Fire:Play()
	local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastP)
	if raycast then
		local instance = raycast.Instance
		local instanceParent = instance.Parent
		local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")
		
		if findHumanoid then
			game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
		end
	end
	shooting = true
	task.wait(2)
	shooting = false
end)

Ok i’ll test that doc

I hate the 30 letter thing

Got it

I really hate the 30 message thing

Tho this only checks if there is no ammo, it doesn’t check if the gun is shooting, ya know stuff like that.

Aight, well sorry for that but I cannot help you with this issue. I tried my best buddy.

Thx man, U solved one of the errors. The gun now does not shoot after reload, Thx so much

Ur actually telling me to do stuff i already tried

But thx anyways i guess

Oooooh, well sorry then I didn’t know.

But i do need to check if shooting is false and reloading is false and if there is ammo to be fired, i’ll keep trying anyways

So here is the updated code

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
raycastfilter.FilterDescendantsInstances = {player.Character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local reloading = false

local function reload()
	tool.Handle.Reload:Play()
	reloading = true
	task.wait(2)
	ammo = max_ammo
	reloading  = false
end

tool.Activated:Connect(function()
	if not shooting and not reloading then
		if ammo == 0 then
			reload()
			return
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(1)
		shooting = false
	end
end)

The gun works i just need to do some stuff with it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.