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 7 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 7 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.

01--// Declarations
02local Player = game:GetService("Players").LocalPlayer
03repeat wait() until Player.Character ~= nil
04local Character = Player.Character
05 
06local Backpack = Player.Backpack --> This is where all unused tools will be kept.
07local Selected --> This will be where we store our current held tool.
08 
09local function CheckAmount(Object)
10    Selected = Object --> Can be used to swap out the current selected weapon, once you code the rest of the script to switch them.
11    for ToolIndex,Tool in pairs(Backpack:GetChildren())do
12        if ToolIndex > 1 then --> Ensures that there are no than two more other tools in the backpack.
13            Tool:Destroy()
14        end
15    end
16end
17 
18Backpack.ChildAdded:Connect(CheckAmount)
19Character.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.

01local WeaponFolder = workspace.WeaponsFolder
02local MaxDistance = 10
03 
04for _,Weapon in pairs(WeaponFolder:GetChildren())do
05    local Magnitude = (Character.HumanoidRootPart.Position -Weapon.Handle.Position).magnitude
06    if Magnitude <= MaxDistance then
07        -- Insert Instructions
08        break --> Don't want the code to continue running if you found a weapon near you.
09    end
10end
Ad

Answer this question