How to replace an item instead of destroy it? (video + script)

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)
2 Likes
-- 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)
1 Like
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)
1 Like

Thank you for ur script but, there is an error in the script at this line in the if :
if child == addedChild then continue end

error = "expected ‘do’ when parsing for loop, got ‘if’

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?

There is an error in this script ?
error

i missed the do

-- 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

oh yer i forgot that when you equip the item goes into the character

unfortunatly it doesn’t work

Edit : But thank you very much for your help

Working Script:

This Is A Local Script In StarterCharacterScripts

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)

Edit: Comments have been added to the script

1 Like

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)
1 Like

No unfortunatly…

did this part get printed to the output?

print(addedChild.Name, "Added")
1 Like

No, nothing in the output , thank u for helping me

if you do this when you pickup a item what does it print?

game.Players.PlayerAdded:Connect(function(player)
    while true do
        task.wait(1)
        print(#player.Backpack:GetChildren())
    end
end)
1 Like

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