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

How can I make it so that you can only have 2 tools in your backpack?

Asked by 6 years ago

So, I am making a game COMPLETELY based off of Bungie's, "Halo: Combat Evolved" and it is a first person shooter. In the game you can only have 2 weapons, and if you wanted a different weapon, you can swap the current equipped weapon for the one that you want. How would I replicate this? Any help would be appreciated!

1 answer

Log in to vote
0
Answered by 6 years ago

Hello there! First off the way I would replicate what you're trying to accomplish would be to check the amount of tools in the backpack. Once you do that, you want to remove all extra tools. I won't code the entire thing for you but I will give you a base of code that would work in your favor. To clarify, this is a LocalScript and a ServerScript will not work under Filtering Enable Conditions.

--// Declarations
local Player = game:GetService("Players").LocalPlayer
repeat wait() until Player.Character ~= nil
local Character = Player.Character

local Backpack = Player.Backpack --> This is where all unused tools will be kept.
local Selected --> This will be where we store our current held tool.

local function CheckAmount(Object)
    Selected = Object --> Can be used to swap out the current selected weapon, once you code the rest of the script to switch them.
    for ToolIndex,Tool in pairs(Backpack:GetChildren())do
        if ToolIndex > 1 then --> Ensures that there are no than two more other tools in the backpack.
            Tool:Destroy()
        end
    end
end

Backpack.ChildAdded:Connect(CheckAmount)
Character.ChildAdded:Connect(CheckAmount)

As you can see, in this small block of code I've already solved one of your problems. Now the second thing you must go into finding out is swapping weapons that I assume you want to swap with on the ground. For dropped weapons, I would have NPC's or however you want them on the ground placed in a folder in the workspace. There you can get the distance between you and the weapon itself. Here is an example.

local WeaponFolder = workspace.WeaponsFolder
local MaxDistance = 10

for _,Weapon in pairs(WeaponFolder:GetChildren())do
    local Magnitude = (Character.HumanoidRootPart.Position -Weapon.Handle.Position).magnitude
    if Magnitude <= MaxDistance then
        -- Insert Instructions
        break --> Don't want the code to continue running if you found a weapon near you.
    end
end
Ad

Answer this question