So I’m making a gun script for my game and I came to the part of the ammunition,everything works perfectly but what I’m having trouble with is the ammo box,which is an Item that on touch disappears and gives the player 30 bullets on the inventory.
When I test the script and touch a ammo box I get the 30 bullets,however when I spawn another one and I touch it it does’nt work anymore,I know that this is because the script only detects the ammo box once,I used a while loop so the ammo box variables always updates but the problem still remains.
Is there a way to fix this?.
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local M1911A1 = script.Parent
local debounce = false
local hum = player.Character.Humanoid
--------------------------------
local MousePosition = Mouse.Hit.Position
local BT = game.ReplicatedStorage.BulletTrail
local char = player.Character
-----------------------------------------------Ignore dis
local MouseIcon = true
UIS.InputBegan:Connect(function(inp, p)
if inp.UserInputType == Enum.UserInputType.MouseButton2 and MouseIcon == true then
MouseIcon = false
elseif inp.UserInputType == Enum.UserInputType.MouseButton2 and MouseIcon == false or inp.UserInputType == Enum.UserInputType.MouseButton1 and MouseIcon == false then
MouseIcon = true
end
end)
---------------------------------------Animations
local Animation1 = M1911A1.EmptyReload
local Animation2 = M1911A1.GunReload
local Animation3 = M1911A1.Shooting
--------------------------------
local EmptyRl = hum:LoadAnimation(Animation1)
local Reload = hum:LoadAnimation(Animation2)
local Soot = hum:LoadAnimation(Animation3)
-------------------------------------------Ammunition variables
local EmptyAmmo = 7
local ammo = 7
local maxammo = 8
local reloading = false
local Inventoryammo = 0
local MaxInventoryAmmo = 200
local ammotosubtract = 0
local ammoboxcooldown = true
local waitiningforammobox = true
---------------------------------------------------------reloading function--------------------------------------------------------------
local function reload()
if reloading == false and ammo == 0 and player.Character.Humanoid.WalkSpeed == 16 and M1911A1.Equipped and MouseIcon == true and Inventoryammo > 0 then
reloading = true
M1911A1.SoundEffects["M1911 Reload"]:Play()
M1911A1.SoundEffects["M1911 Reload"].Volume = 0.4
EmptyRl:Play()
task.wait(1.7)
ammo = EmptyAmmo
Inventoryammo -= 7
player.PlayerGui.Ammo.AmmoBackGround.AmmoCount.Text = ammo.."/"..Inventoryammo
reloading = false
elseif reloading == false and ammo > 0 and ammo < 8 and player.Character.Humanoid.WalkSpeed == 16 and M1911A1.Equipped and MouseIcon == true and Inventoryammo > 0 then
reloading = true
M1911A1.SoundEffects["pistol_reload1"]:Play()
M1911A1.SoundEffects["pistol_reload1"].Volume = 0.3
Reload:Play()
task.wait(1.2)
ammotosubtract = maxammo - ammo
Inventoryammo -= ammotosubtract
ammo = maxammo
player.PlayerGui.Ammo.AmmoBackGround.AmmoCount.Text = ammo.."/"..Inventoryammo
reloading = false
end
end
--------------------------------------------------------Shooting function----------------------------------------------------------------
M1911A1.Activated:Connect(function()
if debounce == false and ammo > 0 and reloading == false and MouseIcon == true and hum.WalkSpeed == 16 then
debounce = true
ammo -= 1
-------------------------------------------------------------------------------------------------------
M1911A1.Shoot:FireServer(Mouse.Hit.Position)
M1911A1.SoundEffects["M1911 Gunshot"]:Play()
M1911A1.SoundEffects["M1911 Gunshot"].Volume = 0.5
Soot:Play()
player.PlayerGui.Ammo.AmmoBackGround.AmmoCount.Text = ammo.."/"..Inventoryammo
task.wait(0.2)
debounce = false
elseif debounce == false and ammo == 0 and reloading == false and MouseIcon == true and hum.WalkSpeed == 16 then
M1911A1.SoundEffects["clip_empty"]:Play()
M1911A1.SoundEffects["clip_empty"].Volume = 0.5
end
end)
-----------------------------------------------------Reloading Key
UIS.InputBegan:Connect(function(inputObject, isTyping)
if isTyping then return end
if inputObject.KeyCode == Enum.KeyCode.R then
reload()
end
end)
------------------------------------------------Dis is where the problem is at-----------------------------------------------------------
while true do
wait(2)
if waitiningforammobox == true then
local AmmoBox = game.workspace:FindFirstChild("AmmoBox") or game.workspace:WaitForChild("AmmoBox")
local BoxLowerPart = AmmoBox:FindFirstChild("Lower") or AmmoBox:WaitForChild("Lower")
waitiningforammobox = false
------------------------------------------------Make the ammo box give ammo on touch function--------------------------------------------
BoxLowerPart.Touched:Connect(function()
if ammoboxcooldown == true then
ammoboxcooldown = false
AmmoBox:Destroy()
Inventoryammo += 30
if M1911A1.Equipped then
player.PlayerGui.Ammo.AmmoBackGround.AmmoCount.Text = ammo.."/"..Inventoryammo
end
end
ammoboxcooldown = true
waitiningforammobox = true
end)
end
end