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)