Limited Number of Items in Inv

Hello, so I have a button players can press to give them a football. But the issue is that they can spam and have like a million of them. The button works fine, just is there any way I can limit a players inv to having 1 football. A way where if a player has a football, the button recognizes it and does not let them have another.

Here is what I have:

image
image
image

Check if they have the item, try creating a table, and inserting the football inside, when clicking check if the Item is inside the table, if yes, it wont do anything, if no, it will give it.

This is common thing people do to check if they have an item, like if you bought a one time thing, like a gun, why would you buy the same gun again if you already have it?

To Prevent people from buying the item again, we will check if they have it by checking there items, if its found, we won’t give it to them, if it isn’t, we will, we will use table.find() for this:

OwnedItems = {}

btn.Activated:Connect(function()
    if table.find(OwnedItems, Football) then
        return
    else
        table.insert(OwnedItems, Football) -- do this on the Server, not the Client
        return give:FireServer()
    end
end)

Is this code all I need to fix my issue?

I would Recommend setting this up for each Player if you want them to have the same item, if you want them to share, them this would work, do this on the Server, Exploiters can easily exploit this if on the Client

The football is not something the player has to buy. It is just a button they can click to equip another football.

You could also check if the player has the football, seems like the ball is a TOOL then you should check if the ball is on his backpack or Character then if its true dont run the code

This is a bad idea, if the Player is holding the item, the code wont work.

If he is holding then the parent of tool should be the character.

Then you should Probably have a system to set up what a player has,

for example:

Storage = {}

game.Players.PlayerAdded:connect(function(p)
    Storage[p.UserId] = {} -- creates a Table to Store Items
end)
game.Players.PlayerRemoving:connect(function(p)
    Storage[p.UserId] = nil -- removes Table
end)

And on the RemoteEvent:

     if table.find(Storage[p.UserId], Football) then
        return
    else
        table.insert(Storage[p.UserId], Football)
        -- code to give item
    end

The simplest way of doing this is checking if the backpack has a football, and if it does, it won’t fire the server.

local player = game.Players.LocalPlayer

btn.MouseButton1Click:Connect(function()
     if not player.Backpack:FindFirstChild("Football") then -- checks if there isn't a football
          give:FireServer()
     end
end)

However, WARNING, you should probably also ALWAYS put as check on the server as well, otherwise exploiters can fire the event anyway. Something like this:

game.ReplicatedStorage.GiveFootBall.OnClientEvent:Connect(function(player)
     if not player.Backpack:FindFirstChild("Football") then -- checks if there isn't a football
          -- code that gives football
     end
end)

2 Likes

Where would the first local script that you provided go?

It would go in the same place you screenshotted.
Also, @DasKairo is right. If the player is holding the item, the tool would no longer be in the backpack, but instead the character. You would also have to place a check on the character to see if “Football” is inside the character. Something like this:

-- checks if football isn't in the backpack AND isn't in the character
if not player.Backpack:FindFirstChild("Football") and not player.Character:FindFirstChild("Football") then

true, but there is one issue to this, the Player can just drop the Tool, if they do, they can get another football, and have duplicates

So both codes you sent would be in the same local script?

No, the second script would be in the server script that actually gives the football

1 Like

True, however they only specified the problem to be within only having one in the inventory

1 Like

I turned off the feature to drop the football.

1 Like

This system might work for what he is trying to do, but what you have is a simple system, if you want a Inventory as in you cant get an item again, this would work for that


@develofied In that case, both codes should work, @1nicopatty’s code would work the best if so

1 Like

Do I add the script to mine, or replace it with what I have?

Do I place the first script in with the ServerScript?