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

How do I make a crate limit the amount of items it gives?

Asked by 4 years ago

I've been trying to make a script that gives a gun until you have no guns left and refreshes in an hour. It gives the gun, but never stops giving it even though I'm trying to make it so when the Amount = 0 it won't give them anymore. Script:

local Model = script.Parent
local Base = Model.PrimaryPart
local ClickDetector = Base:WaitForChild("ClickDetector")
Amount = 2

local ToolName = "Beretta M9" --the name of the tool the player gets
local WhitelistGroupIds = { 
    5310854, --ID Goes Here
}

function GiveTool(plr)
    if Amount == 2 then
        game:GetService("ServerStorage"):FindFirstChild(ToolName)
        game:GetService("ServerStorage")[ToolName]:Clone().Parent = plr.Backpack
        Amount = 1
        elseif Amount == 1 then
        game:GetService("ServerStorage"):FindFirstChild(ToolName)
        game:GetService("ServerStorage")[ToolName]:Clone().Parent = plr.Backpack
        Amount = 0
    elseif Amount == 0 then
            print ("Out of guns.")
            end
        end

ClickDetector.MouseClick:connect(function(plr)
    if #WhitelistGroupIds > 0 then
        for i=1, #WhitelistGroupIds do
            if plr:IsInGroup(WhitelistGroupIds[i]) then
                GiveTool(plr)
            end
        end
    else
        GiveTool(plr)
    end
end)






    if Amount == 2 then game.Workspace.Houses.ClassifiedTurf.GUIPart.BillboardGui.TextLabel.Text = "Beretta M9 2/2"
    end
    if Amount == 1 then game.Workspace.Houses.ClassifiedTurf.GUIPart.BillboardGui.TextLabel.Text = "Beretta M9 1/2"
    end
    if Amount == 0 then game.Workspace.Houses.ClassifiedTurf.GUIPart.BillboardGui.TextLabel.Text = "Beretta M9 0/2"
    end

while true do
wait (3600)
Amount = 2
end

0
Why not always check if amount is always > 0? killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
4 years ago

Try this, hope it helps. :D

local clickdetector = script.Parent.PrimaryPart:WaitForChild("ClickDetector")
local textlabel = game.Workspace.Houses.ClassifiedTurf.GUIPart.BillboardGui.TextLabel
local amount = 2

local ToolName = "Beretta M9"
local WhitelistGroupIds = { 
    5310854, --ID Goes Here
}

if Amount == 2 then 
    textlabel.Text = "Beretta M9 2/2"
    else if Amount == 1 then 
        textlabel.Text = "Beretta M9 1/2"
    else
        textlabel.Text = "Beretta M9 0/2"
    end
end

local function onmouseclick (player)
    if #WhitelistGroupIds > 0 then
        for i=1, #WhitelistGroupIds do
            if player:IsInGroup(WhitelistGroupIds[i]) then
                if amount >= 1 then
                    game:GetService("ServerStorage"):FindFirstChild(ToolName)
                    game:GetService("ServerStorage")[ToolName]:Clone().Parent = player.Backpack
                    amount = amount - 1
                else
                    print ("Out of guns.")
                end
            end
        end
    else if amount >= 1 then
        game:GetService("ServerStorage"):FindFirstChild(ToolName)
        game:GetService("ServerStorage")[ToolName]:Clone().Parent = player.Backpack
        amount = amount - 1
        else
            print ("Out of guns.")
        end
    end

    if Amount == 2 then 
        textlabel.Text = "Beretta M9 2/2"
        else if Amount == 1 then 
            textlabel.Text = "Beretta M9 1/2"
        else
            textlabel.Text = "Beretta M9 0/2"
        end
    end

end

clickdetector.MouseClick:Connect(onmouseclick)

while true do
    wait (3600)
    Amount = 2
    textlabel.Text = "Beretta M9 2/2"
end
Ad

Answer this question