Remove all players items from inventory

I know this is probably really easy but I’m new to scripting. I have a door that can be opened using a key that players can get from the workspace. I just don’t know how to make the key get removed from everyone’s backpack when the door opens. Can anyone help? Thanks!

This might not be needed, but here’s the code for my door script.

local debounce = false
local opened = false

script.Parent.Touched:Connect(function(obj)
	if obj.Parent.Name == 'Key' and not debounce then
		if opened == false then
			debounce = true
				for i = 1,200 do
				game:GetService("RunService").Heartbeat:Wait()
				script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent.PrimaryPart.CFrame * CFrame.Angles(0, -0.01, 0))
			end
			opened = true
			debounce = false
		end
	end
end)

If this is on the server then

for _,plr in pairs(game:GetService("Players"):GetPlayers()) do
   for _,Item in pairs(plr.Backpack:GetChildren()) do
      Item:Destroy()
   end
end

Sorry I’m new what would go in the _ and what would the v in (v.Backback:GetChildren()) stand for

the _ would normally be an i which stands for index but we don’t need that so i changed it to _ (the index would be the backpack) and the v stands for value and the value would be the tool

its a loop, it will loop through every children of the backpack, v is the value, and i is the index,
you can also do this

for i,v in pairs(game.Players:GetPlayers()) do
v.Backpack:ClearAllChildren()
end

Didnt know that function existed lol you learn something new every day

1 Like

this code will return an error you forgot to add the parentheses () Item:Destroy()

So I don’t think I was supposed to do this, but I changed the “v” to “tool” and now it’s saying Backpack is not a valid member of Tool "Workspace.Tools.Key

Use ayoub’s code, mine is riddled with mistakes bc I wrote it on mobile

1 Like

So what would I do if I only want to remove the key instead of all of the things in the backpack?

if you’re changing the value you must change its name when you’re indicating it too

 for _,Tool in pairs(plr.Backpack:GetChildren()) do
     Tool:Destroy()
   end

Btw need to change v to Player

local KeyName = "Key" -- YourToolName Here
for _,Player in pairs(game.Players:GetPlayers()) do
for _,Tool in pairs(Player.Backpack:GetChildren()) do
if Tool:IsA("Tool") and Tool.Name == KeyName then
Tool:Destroy()
end
end
end