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

How do I make it so you can only pick up one instance of a tool?

Asked by
Sam4550 42
10 years ago

For example, if I had a bunch of tools lying on the floor called 'Pistol', how would I make it so once a player has one in their inventory, they can't pick up another?

Thanks, - Sam4550.

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

Well, I'd put the tool as a model in Workspace, not the actual Tool itself. So, you'd have a model that you can't pick up. Then, from there, I'd add a Touched event and put it inside the biggest part if the model. Then, once I have my Touched Event laid out, I'll use the FindFirstChild Method to check if the Player already has it. If not, it'll then clone it and put it in their Backpack.

Tool = "Tool name here"

script.Parent.Touched:connect(function(hit) -- Touched Event w/ the connection line
    if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if it's a player
        p = game.Players:GetPlayerFromCharacter(hit.Parent) -- Sets the player variable
        if not p.Backpack:findFirstChild(Tool) then -- Checks if it is in their backpack. If not, then do the following
            copy = game.ServerStorage[Tool]:clone() -- clone the tool
            --copy2 = game.ServerStorage[Tool]:clone()
            copy.Parent = p.Backpack -- put it in their backpack
            --copy2.Parent = p.StarterGear
            script.Parent.Parent:Destroy() -- Destroys the fake gun (Model). Make sure this leads to the ACTUAL Model
        end
    end
end)
Ad

Answer this question