I've been trying to make this a tool but everything I tried didn't work, as of now it is a brick where a player has to touch it for it to perform what it's intended to do. Any leads would be great.
local AmmoBox = script.Parent
local Enabled = true
--==================================================
--Setting
--==================================================
local Ammo = math.huge --Amount of Ammo to give. Set it to "math.huge" to refill the gun's ammo.
local Cooldown = 10
local GunToRefillAmmo = {
"Pistol",
--Add more gun here if you want
}
--==================================================
local function Enabler(Hit)
if Enabled and Hit and Hit.Parent then
local AmmoRefilled = false
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
for _, GunName in pairs(GunToRefillAmmo) do
local Gun = Player.Backpack:FindFirstChild(GunName) or Player.Character:FindFirstChild(GunName)
if Gun then
local GunScript = Gun:FindFirstChild("GunScript_Server")
local Module = Gun:FindFirstChild("Setting")
if GunScript and Module then
local Module = require(Module)
if GunScript.Ammo.Value < Module.MaxAmmo and Module.LimitedAmmoEnabled then
if Cooldown ~= 0 then
Enabled = false
AmmoBox.Sound:Play()
AmmoBox.Transparency = 1
AmmoBox.CanCollide = false
AmmoBox.SurfaceGui.Enabled = false
local function delayer()
AmmoBox.Transparency = 0
AmmoBox.CanCollide = true
AmmoBox.SurfaceGui.Enabled = true
Enabled = true
end
delay(Cooldown,delayer)
else
AmmoBox.Sound:Play()
end
AmmoRefilled = true
local ChangedAmmo = (Ammo == math.huge or GunScript.Ammo.Value+Ammo >= Module.Ammo) and Module.Ammo or (GunScript.Ammo.Value + Ammo)
GunScript.Ammo.Value = ChangedAmmo
GunScript.ChangeMagAndAmmo:FireClient(Player,Module.AmmoPerMag,ChangedAmmo)
end
end
end
end
end
end
end
AmmoBox.Touched:connect(Enabler)
