game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) repeat wait() until plr.Character:FindFirstChild("Humanoid") while wait() do if char:FindFirstChild("ForceField") then for _,c in pairs(plr.Backpack:GetChildren()) do local bp = c:Clone() c:Destroy() bp.Parent = plr.PlayerGui.tools end else for _,v in pairs(plr.PlayerGui.tools:GetChildren()) do local pb = v:Clone() v:Destroy() pb.Parent = plr.Backpack end end end char.Humanoid.Died:connect(function() for _,c in pairs(plr.Backpack:GetChildren()) do local bbp = c:Clone() c:Destroy() bbp.Parent = plr.PlayerGui.tools end end) end) end) --Broken, it spams after u reset.
I made this script, basically what it does is: if you are inside a forcefield, it clones the items from ur inv, puts them in a folder and the deletes the one in ur inventory. if you are outside the forcefield then it puts your items back in ur inv and deletes them from the folder.
The problem is: it works perfectly until you reset/die, once you die and respawn the script starts to spam the items in your inventory like this: https://gyazo.com/59e1b824d31b546f92898e3e789514e2
You should use ChildAdded and ChildRemoved instead of loops:
local recordedTools = {} game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) char.ChildAdded:Connect(function(child) if child:IsA("ForceField") then local tools = plr.Backpack:GetChildren() local equippedTool = char:FindFirstChildOfClass("Tool") if equippedTool then table.insert(tools,1,equippedTool) end recordedTools = tools for _,v in pairs(tools) do v.Parent = nil end end end) char.ChildRemoved:Connect(function(child) if child:IsA("ForceField") and recordedTools then for _,v in pairs(recordedTools) do v.Parent = plr.Backpack end end end) end) end)