Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I fix this ammo box? [APOLOGIES ERROR FOUND AND FIXED!]

Asked by
painzx3 43
10 years ago

This is the code.

function HandleAmmo(box)
    local Amount = box.Amount
    local GUI = box.GUI
    local AmountLabel = GUI.Frame.Amount
    local Debounce = false

    local function UpdateGUI()
        AmountLabel.Text = Amount.Value .. " ROUNDS"
    end

    UpdateGUI()

    box.Touched:connect(function(hit)
        if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
            local GunScript
            for i, v in pairs(hit.Parent:GetChildren()) do
                if v:IsA("Tool") and v:FindFirstChild("Gun_Script") then
                    GunScript = v.Gun_Script
                    break
                end
            end
            if GunScript then
                Debounce = true
                local Give = GunScript.MaxAmmo.Value - GunScript.StoredAmmo.Value
                local Remaining = Amount.Value - Give
                if Remaining < 0 then
                    Give = Give - math.abs(Remaining)
                end
                Amount.Value = Amount.Value - Give
                GunScript.StoredAmmo.Value = GunScript.StoredAmmo.Value + Give
                UpdateGUI()
                wait(2)
                Debounce = false
            end
        end
    end)
end

function CreateMaxValues(instance)
    for i, v in pairs(instance:GetChildren()) do
        if v:IsA("Tool") and v:FindFirstChild("Gun_Script") and not v:FindFirstChild("MaxAmmo") then
            local Max = Instance.new("IntValue", v.Gun_Script)
            Max.Name = "MaxAmmo"
            Max.Value = v.Gun_Script.StoredAmmo.Value
        else
            CreateMaxValues(v)
        end
    end
end

function GetAllAmmoCrates(instance)
    for i, v in pairs(instance:GetChildren()) do
        if v.Name == "AmmoCrate" and v:FindFirstChild("Amount") then
            HandleAmmo(v)
        elseif v:IsA("Model") then
            GetAllAmmoCrates(v)
        end
    end
end

CreateMaxValues(game.Lighting)
CreateMaxValues(game.StarterPack)
CreateMaxValues(workspace)
CreateMaxValues(game:GetService("Teams"))
GetAllAmmoCrates(workspace)

Workspace.ChildAdded:connect(function(crate)
    if crate.Name == "AmmoCrate" and crate:FindFirstChild("Amount") then
        HandleAmmo(crate)
    end
end)

Answer this question