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

How to remove a tool from all players' backpack??

Asked by
Dekishi 12
4 years ago

Currently working on a round based fighting game. I already have a round system set in place, as well as a script that gives all players a knife at the start of the round. However, I do not know how to remove it from all player's backpacks at the end of the round. What is the correct scripting for this? Thanks.

3 answers

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
4 years ago

Iterate through all the Players' backpacks and Remove their tools.

local function removeTools()
    local players = game.Players:GetPlayers() -- Table of all players in the game
    for _, player in pairs(players) do -- Use a for loop to iterate through the players table
        local backpack = player:FindFirstChild("Backpack") -- Locate the backpack
        if backpack then -- If the backpack was succesfully found...
            backpack:ClearAllChildren() -- Remove everything from their Backpack
        end
    end
end


removeTools()
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

This code should work...

local player = game.Players:GetPlayers()
local backpack = player:WaitForChild("Backpack") 
local character = player.Character or player.CharacterAdded:Wait()

local Itemstodestory = {"TOOL1", "TOOL2", "TOOL3", "TOOL4", "TOOL5"}

local function Desteoyfunction(tools)
    for _, launcher in pairs(Itemstodestory) do
        local tool = tools:FindFirstChild(Desteoyfunction)
        if tool and tool:IsA("Tool") then
            tool:Destroy()
        end
    end
end

Desteoyfunction  (backpack)
Desteoyfunction  (character)

if you want to clear the children of the tools then do this

for _,v in pairs(game.Players:GetPlayers()) do
    v.Backpack:ClearAllChildren() 
end
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This should work:

for _,v in pairs(game.Players:GetPlayers()) do
local bp = v:FindFirstChild("Backpack")
if bp then
bp:ClearAllChildren()
end
end

If the Item is equiped:

for _,w in pairs(game.workspace:GetDescendants()) do
if w:IsA("Tool") and game.Players:FindFirstChild(w.Parent.Name) then
w:Destroy()
end
0
Thanks! Dekishi 12 — 4y
0
c : itz_rennox 412 — 4y

Answer this question