Does anyone know how to make clear inventory zone? That way when you are in zone you will have clear inventory and when not you will get your items back after leaving it. (Zone will be basically invisible part)
I tried to look for solution for tens of minutes, but only found scripts to clear inventory when touched or tutorials for non-combative zones.
local Players = game:GetService("Players")
local part = script.Parent
part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
for _, Player in pairs(game.Players:GetPlayers()) do
for _, Tool in pairs(Player.Backpack:GetChildren()) do
if Tool:IsA("Tool") then
Tool:Destroy()
end
end
end
end
end)
There is one small problem, I need also that the item that player is holding will be also removed and then when player will leave the zone he will get also that item back. Right now when player is holding item and enters the zone he will still have the item.
using .TouchedEnded is dodgy and you should avoid it, aside from it almost being an inappropriate solution to your problem.
Instead, you should use GetPartBoundsInBox(). Create two tables to represent “Time 1” and “Time 2”. Scan the box using the function I mentioned, and put those parts in the table “Time 2”. Scan that table for players. If you find a player inside it, that means that they are inside the box you are scanning.
Players can only have one tool equipped at a time. When a player initially enters the box, search their Character for a tool. If you find one, reparent it to their Backpack. This means that they also can’t have a tool equipped when they walk into the box.
You do not need to delete all their tools. simply set: StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false). This will hide the toolbar and prevent the player from equiping their tools, and it’s a way better solution than the ones the other users have provided here. Again, this means you do not need to delete all thier tools.
Now, maybe now that a second has passed, scan your box once again. This time, put all the parts in table “Time 1.” Now, compare table “Time 1” and “Time 2”. If a player is in “Time 2” and not in “Time 1”, that means that they have moved outside of the zone. Check to see if that player is still in the game, and if they are, do StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) to reenable their toolbar.
I highly recommend you change your script to use my method.
local Game = game --Local environment integration.
local Workspace = workspace
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local Part = Workspace.Part --Zone.
local PlayersTable = {} --Table of players and their tools.
local function OnPlayerRemoving(Player) --'PlayerRemoving' callback function.
PlayersTable[Player] = nil --Remove player from table if they leave the experience.
end
local function OnHeartbeat()
local Parts = Workspace:GetPartsInPart(Part) --Get parts in the part which represents the zone.
for _, Part in ipairs(Parts) do --Iterate over the array of parts.
local Model = Part:FindFirstAncestorOfClass("Model") --Get model from part.
if not Model then continue end
local Player = Players:GetPlayerFromCharacter(Model) --Get player from model.
if not Player then continue end
if PlayersTable[Player] then continue end --Skip if player already has an entry in the table.
local Backpack = Player:FindFirstChildOfClass("Backpack") --Get backpack from player.
if not Backpack then continue end
PlayersTable[Player] = {} --Assign an empty table to the player's table entry.
local Children = Backpack:GetChildren()
for _, Child in ipairs(Children) do
Child.Parent = nil
end
local Item = Model:FindFirstItemWhichIsA("BackpackItem")
if Item then Item.Parent = nil end
PlayersTable[Player].Backpack = Children --Get backpack's children and store it with the player's table entry.
PlayersTable[Player].Character = Item --Get character's equipped tool and store it with the player's table entry.
end
for Player, Items in pairs(PlayersTable) do --Iterate over the 'PlayersTable' dictionary.
local Backpack = Player:FindFirstChildOfClass("Backpack")
if not Backpack then continue end
local Character = Player.Character
if not Character then PlayersTable[Player] = nil continue end --If player has no character then remove their table entry.
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if (not Humanoid) or Humanoid.Health <= 0 then PlayersTable[Player] = nil continue end --If player's character has no humanoid or their humanoid is in a 'died' state then remove their table entry.
local State = false --State variable.
for _, Child in ipairs(Character:GetChildren()) do --Iterate over character's children.
if not (Child:IsA("BasePart")) then continue end --If child is not a part then skip to next iteration.
if table.find(Parts, Child) then State = true break end --If child is a member of the 'Parts' array then switch the state variable and break the loop.
end
if State then continue end --Player is still in the zone so skip to the next iteration.
for _, Child in ipairs(Items.Backpack) do --Iterate over player's stored backpack contents.
Child.Parent = Backpack --Parent each child back to the player's backpack.
end
if Items.Character then Items.Character.Parent = Character end --Re-parent equipped tool back to the player's character.
PlayersTable[Player] = nil
end
end
Players.PlayerRemoving:Connect(OnPlayerRemoving)
RunService.Heartbeat:Connect(OnHeartbeat)
One issue with ‘SetCoreGuiEnabled’ is that it’s a client-exclusive method, and can thus be overidden by an exploiter.
Hello, thanks a lot, I have few issues with understanding some things. Can you please create place where the system is already setuped? It would help me a lot, thanks!
This isn’t an argument. Regardless of what you tell the client to do, they can just clone and respawn their tools once they enter the zone anyways. That overrides any method anybody has provided here.
Instead of using the bizarre method that everyone here seems to be inclined to use, just instead set up a ChildAdded listener to the player’s character, and that means that any future gear will be detected and can instantly be reparented to the backpack, on top of my method.