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

stop player from equipping nothing?

Asked by 3 years ago

I'm making an fps game but at the moment a player can unequip a tool, how do I make it so the player cant unequip a tool but can change tools.

1 answer

Log in to vote
0
Answered by 3 years ago

To do this, Instead of using the Default Player Backpack, you can use replicated storage and move the tool to the character model through a script.

Local Script:

local tools = game.ReplicatedStorage.Tools --Folder in Replicated Storage called Tools
local UserInputService = game:GetService("UserInputService")
local listOfNumbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Zero"}

for i, v in ipairs(tools:GetChildren()) do
    UserInputService.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.Keyboard then
            if input.KeyCode == Enum.KeyCode[listOfNumbers[i]] then --The i is the number to be pressed to get the tool
                if game.Players.LocalPlayer.Character then

                    if game.Players.LocalPlayer.Character:FindFirstChild("curTool") then
                        game.Players.LocalPlayer.Character.curTool:Destroy()
                    end

                    local clone = v:Clone()
                    clone.Name = "curTool"
                    clone.Parent = game.Players.LocalPlayer.Backpack
                    game.Players.LocalPlayer.Character.Humanoid:EquipTool(clone)
                end
            end
        end
    end)
end

while wait() do
    for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
        v:Destroy()
    end
end

This is the complete Script, obviously, you should tweak it to your needs and remove the default player backpack GUI. The tools I used to test were in the Tools folder mentioned on line 1.

I don't think this is the most efficient or easy way to do this, but it's the way I found.

Ad

Answer this question