How can I delete :Clone() function

Hi everyone I’m currently working on an Inventory System and rn I have a Clone function that duplicates my Tools when I die, in the Inventory.Now 2 days ago I deleted the :Clone() function and the Problem has been solved but now I tested it again and it gave me this error:ItemButton is not a valid member of Frame “Players.Nemmo1232.PlayerGui.InventoryGui.InventoryFrame.ItemsFrame”. This is the LocalScript:

local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild("ItemsFrame")

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then
		
		local ItemButton = script.Parent.ItemsFrame.ItemButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = itemFrame
		
		local equipButton = script.Parent.EquipFrame["EquipButton"]
		
			ItemButton.MouseButton1Click:Connect(function()
				script.Parent.EquipFrame.Title.Text = ItemName
				script.Parent.EquipFrame.Title.Visible = true
				equipButton.Visible = true
			end)
	end
	
end)
1 Like

I think the problem is that you do script.Parent Instead of using the actual playerGui. starter GUI is just meant for storing the GUI, and basically giving it to all of the players. try doing this:

local GUI = game.Players.LocalPlayer.PlayerGui.ScreenGUI --whatever your screenGUI is named.
local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild("ItemsFrame")

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then

		local ItemButton = GUI.ItemsFrame.ItemButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = itemFrame

		local equipButton = GUI.EquipFrame["EquipButton"]

		ItemButton.MouseButton1Click:Connect(function()
			GUI.EquipFrame.Title.Text = ItemName
			GUI.EquipFrame.Title.Visible = true
			equipButton.Visible = true
		end)
	end

end)
1 Like

well it does the same I think Ik why.I didn’t show the script that is connected with and don’T I have to delete the clone function?

local inventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent

game.Players.PlayerAdded:Connect(function(player)
	
	local inventory = player:WaitForChild("Inventory")
	
	local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()
	
	inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(player, Item.Name, true)
	end)
end)

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)
	
	 if Value == false then
			local SelectedItem = player.Inventory:FindFirstChild(ItemName)
			local backpack = player.Backpack:GetChildren()
			local stuff = player.Character:GetChildren()
			
			if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
				button.Text = "Unequip"
				button.BackgroundColor3 = Color3.new(255,0,0)
				SelectedItem:Clone().Parent = player.Backpack
			else
				for i,v in ipairs(backpack) do
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0,255,0)
					v:Destroy()
				end
				for i, v in ipairs(stuff) do
					if v:IsA("Tool") then
						button.Text = "Equip"
						button.BackgroundColor3 = Color3.new(0,255,0)
						v:Destroy()
					end
				end
			end
	 end
end)

I just want to delete the Clone function or like stop it from Cloning somehow I remember I had a similar issue once and I think it was in the Script I pasted u

well, In my head, I am thinking that it is duplicating the items because of this:

game.Players.PlayerAdded:Connect(function(player)
	
	local inventory = player:WaitForChild("Inventory")
	
	local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()
	
	inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(player, Item.Name, true)
	end)
end)

when you respawn, it adds children again, hence why it gets duplicated.

1 Like

Is there a way I can fix this somehow?like delete the Clone and Children function.And I have a question does the Clone function in this part:if #backpack == 0 and not player.Character:FindFirstChildWhichIsA(“Tool”) then
button.Text = “Unequip”
button.BackgroundColor3 = Color3.new(255,0,0)
SelectedItem:Clone().Parent = player.Backpack have nothing to do with the Clone issue?

Your problems might be these :slight_smile:

  1. The ItemButton is not a valid member of the Frame named ItemsFrame. This error is occurring in the LocalScript.

  2. The items are being duplicated when the player respawns, as the inventoryEvent is being fired for each child of the InventoryGui on the player’s respawn.

  3. The Clone() function is causing duplication of the tools when the player dies.

local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild("ItemsFrame")

-- Ensure itemFrame is valid
if not itemFrame then
    warn("ItemsFrame not found")
    return
end

-- Clear the inventory when the player respawns
local function clearInventory()
    for _, child in pairs(itemFrame:GetChildren()) do
        if child:IsA("GuiButton") then
            child:Destroy()
        end
    end
end

-- Connect to the player's respawn event
game.Players.LocalPlayer.CharacterAdded:Connect(function()
    clearInventory()
end)

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
    if Value == true and itemFrame:FindFirstChild("ItemButton") then
        local ItemButtonTemplate = itemFrame.ItemButton
        local ItemButton = ItemButtonTemplate:Clone()
        ItemButton.Visible = true
        ItemButton.Name = ItemName
        ItemButton.Text = ItemName
        ItemButton.Parent = itemFrame
        
        local equipButton = script.Parent.EquipFrame:FindFirstChild("EquipButton")
        if equipButton then
            ItemButton.MouseButton1Click:Connect(function()
                script.Parent.EquipFrame.Title.Text = ItemName
                script.Parent.EquipFrame.Title.Visible = true
                equipButton.Visible = true
            end)
        else
            warn("EquipButton not found in EquipFrame")
        end
    elseif not itemFrame:FindFirstChild("ItemButton") then
        warn("ItemButton template not found in ItemsFrame")
    end
end)
1 Like

definitely try what the guy above said. also, I noticed that when you equip a tool, it leaves your backpack, and goes to the character, then when you unequip it, it goes back to the backpack, so that might be another issue you should look out for.

The script executes before the instances you are indexing are replicated on the Player.

You are trying to Clone() nil, it isn’t the built in function’s fault.

Considering you do not use any WaitForChild() in your code, it would be a good idea for you to look into it and use it. Instance | Documentation - Roblox Creator Hub

I would also suggest you Index the ItemButton at the very top since you are creating a new clone every time the Event is called anyway.

local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:WaitForChild("ItemsFrame")
local ItemButton = itemFrame:WaitForChild("ItemButton")

thank y’all 2 for the response but when I die the Inventory gets cleared fully like all tools in there disappear although I want the Tool that gets duplicated when I die to disappear sry for being annyoing

Make that clone a variable. I don’t know exactly if that’s what its called, but here is what I mean:

local ItemClone = Item:Clone() -- Creates the clone
ItemClone.Parent = game.Workspace -- Moves clone to Workspace
wait(10) -- Wait 10 seconds
ItemClone:Destroy() -- Deletes cloned item

I am not a hundred percent sure this is what you are after, but this is from my understanding

1 Like

Probably just need to disable ResetOnSpawn on the ScreenGui

ScreenGui | Documentation - Roblox Creator Hub

1 Like

Dude wth I promise this is the last thing ima ask y’all then ima try figure it out by myself but now it gave me an error which says: ServerScriptService.InventoryScript:25: attempt to index nil with ‘Clone’.This is the Script:

local inventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent


game.Players.PlayerAdded:Connect(function(player)
	
	local inventory = player:WaitForChild("Inventory")
	
	local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()
	
	inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(player, Item.Name, true)
	end)
end)

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)
	
	 if Value == false then
			local SelectedItem = player.Inventory:FindFirstChild(ItemName)
			local backpack = player.Backpack:GetChildren()
			local stuff = player.Character:GetChildren()
			
			if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
				button.Text = "Unequip"
				button.BackgroundColor3 = Color3.new(255,0,0)
				SelectedItem:Clone().Parent = player.Backpack
			else
				for i,v in ipairs(backpack) do
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0,255,0)
					v:Destroy()
				end
				for i, v in ipairs(stuff) do
					if v:IsA("Tool") then
						button.Text = "Equip"
						button.BackgroundColor3 = Color3.new(0,255,0)
						v:Destroy()
					end
				end
			end
	 end
end)

Error line 25 need to add checks :white_check_mark:

if SelectedItem then
    -- Existing code to handle cloning and equipping
else
    warn("Item '" .. ItemName .. "' not found in inventory.")
end
1 Like