Hello, I made a script that allows me to have only one item at a time. The problem with this script is that it destroys the new item that I take while I would like it to replace the old item. I would like to know if someone can help me to solve this problem. I have tried several scripts without success. Thanks
Edit: Unfortunately the screen recorder does not record the output, but there is the print(“ERRORRRR”) that appears when I click on the new item
-- Script located in ServerScriptService
local players = game:GetService('Players')
local runService = game:GetService('RunService')
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local bp = player:WaitForChild('Backpack')
bp.ChildAdded:Connect(function(object)
if #bp:GetChildren() < 2 then return end
runService.Heartbeat:Wait()
object:Remove()
print("ERRORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR")
end)
end)
end)
-- Script located in ServerScriptService
-- when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for the backpack folder
local backpack = player:WaitForChild('Backpack')
-- when a child is added to the backpack
backpack.ChildAdded:Connect(function(addedChild)
-- loop every child in the backpack
for i, child in ipairs(backpack:GetChildren())
-- if the child is the same as the new added child then skip
if child == addedChild then continue end
-- destroy the child that is not the the new child added
child:Destroy()
end
end)
end)
plr.Character.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
for _,v in pairs(plr.Backpack:GetChildren()) do
if v ~= tool then v:Destroy() end
end
for _,v in pairs(plr.Character:GetChildren()) do
if v ~= tool and v:IsA("Tool") then v:Destroy() end
end
end
end)
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
for _,v in pairs(plr.Backpack:GetChildren()) do
if v ~= tool then v:Destroy() end
end
for _,v in pairs(plr.Character:GetChildren()) do
if v ~= tool and v:IsA("Tool") then v:Destroy() end
end
end
end)
He just forgot one word, here’s the adjusted script
-- Script located in ServerScriptService
-- when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for the backpack folder
local backpack = player:WaitForChild('Backpack')
-- when a child is added to the backpack
backpack.ChildAdded:Connect(function(addedChild)
-- loop every child in the backpack
for i, child in ipairs(backpack:GetChildren()) do
-- if the child is the same as the new added child then skip
if child == addedChild then continue end
-- destroy the child that is not the the new child added
child:Destroy()
end
end)
end)
Thanks, the script works fine except that once I take an object, then a new one then I can’t take the old object anymore. I know I can destroy it and make it reappear to counter this problem but I guess there must be a less restrictive solution to put in this script?
-- Script located in ServerScriptService
-- when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for the backpack folder
local backpack = player:WaitForChild('Backpack')
-- when a child is added to the backpack
backpack.ChildAdded:Connect(function(addedChild)
-- loop every child in the backpack
for i, child in ipairs(backpack:GetChildren()) do
-- if the child is the same as the new added child then skip
if child == addedChild then continue end
-- destroy the child that is not the the new child added
child:Destroy()
end
end)
end)
Try it again I made a slight typo, also this will only work if the player doesn;t currently have the other tool equipped, let me make you a script that takes both into account
local char = script.Parent
local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
backpack.ChildAdded:Connect(function(newChild) -- Listen for children Being added to the backpack
for _, child in pairs(player.Character:GetChildren()) do -- When tools get equipped they go to the character, so check the character for equipped tools
if child:IsA("Tool") and child ~= newChild then child:Destroy() end -- Destroy any other tools
end
for _, child in pairs(backpack:GetChildren()) do -- Go throught the backpack
if child == newChild then continue end -- If the current tool is the new tool skip the loop iteration
child:Destroy() -- Else destroy the tool
end
end)
-- Do the same thing here but for when a tool gets equipped in the player (or if you pick a tool up and it gets equipped automatically)
char.ChildAdded:Connect(function(newChild)
for _, child in pairs(char:GetChildren()) do
if child:IsA("Tool") and child ~= newChild then child:Destroy() end
end
for _, child in pairs(backpack:GetChildren()) do
if child == newChild then continue end
child:Destroy()
end
end)
Thank you for your time.
As for FartFella’s script, it works but the problem is that as the old object is destroyed I can’t pick it up anymore. The solution is to create a regular respawn of the object? Or a respawn as soon as the object is destroyed?
Even with Remove() it doesn’t work (to take the old object). I tried some scripts to respawn the item but with no success. If anyone has any help that would allow the item to be respawned once it is destroyed?
does this work?
does it print the item name in the output?
-- Script located in ServerScriptService
-- when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for the backpack folder
local backpack = player:WaitForChild("Backpack")
-- when a child is added to the backpack
backpack.ChildAdded:Connect(function(addedChild)
print(addedChild.Name, "Added")
-- drop any tools
for i, child in ipairs(player.Character:GetChildren()) do
if child:IsA("Tool") == false then continue end
child.Parent = game.Workspace
end
-- loop every child in the backpack
for i, child in ipairs(backpack:GetChildren()) do
-- if the child is the same as the new added child then skip
if child == addedChild then continue end
-- drop the old child
child.CFrame = player.Character.PrimaryPart.CFrame + player.Character.PrimaryPart.CFrame.LookVector * 5
child.Parent = game.Workspace
end
end)
end)
I try to make an screen with the output but for some reason it doesn’t work.
so as soon as I get into the game it puts me : 0 then 0(x2) then 0(x3) …
and if i click on the green part it put : 1 then 1(x1) then 1(x2) …
if i click again : 2 then 2(x1) then 2(x2) …
the brown part does not respond at all
Sorry if my explanations are not clear, I don’t speak English well and it’s hard to describe the situation