My gun reload script reloads all weapons

hi, i’m trying to make a mad paintball style game but with actual guns instead of paintball guns. i’ve made two tools so far (a pistol and a shotgun) and they both have the same localscript but with a few minor changes, such as delay time and the amount of shots it can fire. here is the reload function i’ve used:

local function reload()
	reloading = true
	reloadtext.Visible = true
	repeat
		wait(0.1)
		ammo += 1
	until ammo == maxAmmo
	reloading = false
	reloadtext.Visible = false
end

the script does what it should do - load the characters weapon - but instead of loading just the weapon the player has equipped, it loads every weapon in the character’s inventory, which i don’t want.

any help would be much appreciated! let me know if this is in the wrong category or i need to provide more information.

Can you show the whole script?

sure, i’ve just edited the reload function with an if statement but it didn’t change much

local maxAmmo = script.Parent:WaitForChild("MaxAmmo").Value
local ammo = maxAmmo
local reloading = false

local key = script.Parent.Name

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local label = playerGui:WaitForChild(key.."Gui"):FindFirstChild("Background").AmmoLeft
local frame = playerGui:WaitForChild(key.."Gui"):FindFirstChild("Background").Bar
local reloadtext = playerGui:WaitForChild(key.."Gui"):FindFirstChild("ReloadText")

local damagedealt = 6
local mouse
local debounce = false

local function reload()
	if script.Parent.Equipped then
		reloading = true
		reloadtext.Visible = true
		repeat
			wait(0.1)
			ammo += 1
		until ammo == maxAmmo
		reloading = false
		reloadtext.Visible = false
	end
end

script.Parent.Equipped:Connect(function(m)
	mouse = m

	while wait() do
		label.Text = (ammo).."/"..maxAmmo
		frame.Size = UDim2.new(ammo/maxAmmo,0,1,0)
	end
end)

script.Parent.Activated:Connect(function()
	if ammo > 0 and not reloading then
		if not debounce then
			debounce = true
			ammo -= 1
			script.Parent.GunAudio:Play()
			if mouse.Target ~= nil then
				if mouse.Target.Parent:FindFirstChild("Humanoid") then
					if mouse.Target.Name == "HeadHitbox" then
						script.Parent.DealDamage:FireServer(mouse.Target.Parent, damagedealt * 1.5)
					else
						print(mouse.Target.Name)
						script.Parent.DealDamage:FireServer(mouse.Target.Parent, damagedealt)
					end
				end
			end
			wait(.1)
			debounce = false
		end
	end

	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
			reload()
		end
	end)
end)

I think here is your problem.

Bc if this is in 2 spread script or more they all will just listen when the key R is pressed and fire the function,
Wich means all ur weapons will reload.

Mabey try this?

Add a string value in StarerGui, name it mabey
GunToReload

Then add this local GunName = --enter the gun name that this script is into under

Then add this playerGui.GunToReload.Value = GunName under

Then replace

With
if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo and playerGui.GunToReload.Value == GunName then

Do this in all your script and make sure you ender different GunName name on each scripts.

Hoop this helps.

2 Likes

thanks for this! the script finally works now :slight_smile:

2 Likes