But while testing, I realised that whenever you reload and then unequip then gun then requip it the gun uses (probally) multiplies the amount of bullets used.
The LocalScript
local tool = script.Parent
local MaxAmmo = tool:GetAttribute("MaxAmmo")
local ammo = tool:GetAttribute("Ammo")
local reloading = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local ammoDisplay = playergui:WaitForChild("GunGui"):FindFirstChild("Ammo")
local ammoframe = playergui:WaitForChild("GunGui"):FindFirstChild("Frame")
script.Parent.Equipped:Connect(function(mouse)
local function reload()
reloading = true
wait(1)
ammo = MaxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if ammo > 0 and not reloading then
ammo = ammo -1
script.Parent.GunShot:Play()
if mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
ammoDisplay.Text = ammo.." / "..MaxAmmo
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)
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = 'rbxassetid://117431027'
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)
I’ve encountered this problem when I first learned how to script; basically the mistake is connecting a function to an event multiple times.
When you :Connect() Events, you make a function gets called each time the events get fired.
The function that “subscribes” to an event won’t get disconnected until the object responsible for the event is destroyed.
Multiple functions can “subscribe” to an event.
Generally you might want to avoid a
event:Connect(function()
event2:Connect(function()
end
end
unless it’s something like tracking when a child is added and removed again
workspace.TrackObjectsFolder.ChildAdded:Connect(function(child)
print("CHILD ADDED", child)
child.AncestryChanged:Connect(function()
if child:IsDescendantOf(TracksObjectsFolder) then
print("CHILD REMOVED", child)
end
end
end
20:22:19.725 Players.thatrandomnoob23.Backpack.AssaultRifle.MainGun:21: attempt to index nil with 'Target' - Client - MainGun:21
20:22:19.726 Stack Begin - Studio
20:22:19.726 Script 'Players.thatrandomnoob23.Backpack.AssaultRifle.MainGun', Line 21 - Studio - MainGun:21
20:22:19.726 Stack End - Studio
New LocalScript
local tool = script.Parent
local MaxAmmo = tool:GetAttribute("MaxAmmo")
local ammo = tool:GetAttribute("Ammo")
local reloading = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local ammoDisplay = playergui:WaitForChild("GunGui"):FindFirstChild("Ammo")
local ammoframe = playergui:WaitForChild("GunGui"):FindFirstChild("Frame")
local function reload()
reloading = true
wait(1)
ammo = MaxAmmo
reloading = false
end
script.Parent.Activated:Connect(function(mouse)
if ammo > 0 and not reloading then
ammo = ammo -1
script.Parent.GunShot:Play()
if mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
ammoDisplay.Text = (ammo).. " / "..MaxAmmo
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)
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = 'rbxassetid://117431027'
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)