Roblox clear backpack script clears ALL PLAYER'S backpacks

So I have a script that is meant to clear the players backpack on touch, and give them a new gear, but for some reason, it clears every player’s backpack, how do I make it only clear the player who touches the part’s backpack.

local RESET_SECONDS = 2
local isTouched = false  -- Declare debounce variable

function removeItems()
	local Players = game:GetService("Players")
	for _, Player in pairs(Players:GetPlayers()) do
		local Char = Player.Character or Player.CharactedAdded:Wait()
		local GearInPlayer = Player:FindFirstChild("Backpack")
		local GearInCharacter = Char:GetChildren()
		GearInPlayer:ClearAllChildren()
		for _, Tool in pairs(GearInCharacter) do
			if Tool:IsA("Tool") then
				Tool:Destroy()
			end
		end
	end
end

function Touch(hit)
	if not isTouched then
		isTouched = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Tool = player.leaderstats.Pan.Value
		local Pos = game.Workspace.TeleLocation
		removeItems()
		wait(.1)
		local ChosenTool = game.ServerStorage.Pans:FindFirstChild(Tool):Clone()
		ChosenTool.Parent = player.Backpack
		hit.Parent:moveTo(Pos.Position)
		wait(RESET_SECONDS)
		isTouched = false
	else
		warn("This portal is on cooldown")
	end
end
	
script.Parent.Touched:connect(Touch) 
3 Likes

This loops through every player that’s currently in the game rather than the individual player that activated the function. To resolve this, you could send through the specific player to the “removeItems” function so that it only updates their Backpack and Character model:

Example:

local Players = game:GetService("Players")
local RESET_SECONDS = 2
local isTouched = false

local function removeItems(player)
    local Backpack = Player.Backpack
    local Character = player.Character or player.CharacterAdded:Wait()

    -- Continue
end

local function Touch(hit)
    if not isTouched then
        isTouched = true

        local player = Players:GetPlayerFromCharacter(hit.Parent)

        if player then
            removeItems(player)
            -- Continue
        end
    end
end

script.Parent.Touched:Connect(Touch)
2 Likes

I don’t understand sorry, could you please elaborate?

Nevermind, it works, thank you very much!

2 Likes

Sure! I’m not sure what specifically you have a question about, so I’ll explain as much as I can.


In your original codeblock, the “removeItems” function has a loop that runs code for every player in the Players service:

That means that whenever the “removeItems” function was activated from the “Touched” function, it would tell the game to do the following actions for every player in the game:

	local Char = Player.Character or Player.CharactedAdded:Wait() -- References the player's Character
		local GearInPlayer = Player:FindFirstChild("Backpack") -- References the player's Backpack
		local GearInCharacter = Char:GetChildren() -- Creates a table or a "list" of every part on the first layer of the Character
		GearInPlayer:ClearAllChildren() -- This clears every item in the player's Backpack
		for _, Tool in pairs(GearInCharacter) do -- This would look through the "list" of items in the Character model that was created earlier
			if Tool:IsA("Tool") then -- If the script finds a Tool on the first layer of the Character model...
				Tool:Destroy() -- The item is destroyed
			end
		end
	end

Because you wanted to make sure that the Tools are only removed from the player that touched the part, we need to make sure that the “removeItems” function only runs the code for that player and not every player in the game.

Fortunately, there’s a variable that was already included in the “Touched” function from your original codeblock that tells us exactly which player touched the part:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

From here, we can use that variable to tell the “removeItems” function which player it needs to remove the Tools from.

It’s like turning in your homework at school – if the name of the person isn’t on the homework, the teacher won’t know who’s work it is. By adding “player” into the parentheses when activating a function, we can tell the function which player the script needs to remove the tools from:

-- The code goes from this:

removeItems() -- This activates the "removeItems" function

-- To this:

removeItems(player) -- This activates the "removeItems" player AND tells the function which player the script needs to remove the tools from

Even though you got it to work before I completed this post, I hope that my explanations are still useful! :slight_smile:

1 Like

Thank you, this is useful! abcdefghijklmnop

1 Like