Item not cloning to backpack

When the player clicks the button to equip the sword. I want to check if there is already a sword in the backpack, if there is already a sword there. the player will not be able to equip one more sword. (Can only contain 1 sword in the backpack. But is not equipling any sword. What could be wrong?

local replicatedStorage = game:GetService("ReplicatedStorage")
local equipSword = replicatedStorage.Swords:FindFirstChild("Equip")
local sword = replicatedStorage.Swords

local toolnames = {
	"RainbowSword",
	"DiamondSword"
}
equipSword.OnServerEvent:Connect(function(player, itemsword)
	
	for _,tool in pairs(player.Backpack:GetChildren()) do
	if table.find(toolnames,tool.Name) then
		print("Sword find")
	else
			
	local clone = sword[itemsword]:Clone()
	clone.Parent = player.Backpack

	local clone = sword[itemsword]:Clone()
	clone.Parent = player.StarterGear
			
end				
end		
end)
3 Likes

This darn TypeError why

Only thing I can think of is that you’re referencing the itemsword parameter incorrectly, or the Sword you’re searching for isn’t in the right place

Btw are you getting any errors at all? If you are, please show the Output

1 Like

nope, any error. that’s why.

the itemsword is on localscript (button)

when i click a especific button, the itemsword send the name of the tool to the SSS script (to equip) that sword name. But before equipping i want to check if already have another Sword.Name there. To not be able to get 2 swords in backpack

Btw, i added a sword in the starterPack to test the Print("sword find)
and if i have a sword there, its printing.

but if i dont have sword there, is not cloning

The only instance I could really see then, is that the itemsword isn’t being referenced correctly

Can you try this and see if you get a different outcome?

local replicatedStorage = game:GetService("ReplicatedStorage")
local equipSword = replicatedStorage.Swords:FindFirstChild("Equip")
local sword = replicatedStorage.Swords

local toolnames = {
	"RainbowSword",
	"DiamondSword"
}
equipSword.OnServerEvent:Connect(function(player, itemsword)
	print(itemsword)

	for _,tool in pairs(player.Backpack:GetChildren()) do
	    if table.find(toolnames,tool.Name) then
		    print("Sword find")
	    else
			
        print("Cloning")

	    local clone = sword:WaitForChild[itemsword]:Clone()
	    clone.Parent = player.Backpack

	    local clone = sword:WaitForChild[itemsword]:Clone()
	    clone.Parent = player.StarterGear
			
        end				
    end		
end)

when i click equip, is printing: print(itemsword)

but not print Cloning

maybe this:

for _,tool in pairs(player.Backpack:GetChildren()) do
	    if table.find(toolnames,tool.Name) then
		    print("Sword find")
	    else

is be using wrong, there is another way to check tools.Name in backpack?

I don’t think that’d be the case, since you’re properly checking for every Player’s Tools using table.find()

Only thing I can think of printing the tool variable provided inside your loop

i will try check the backpack in the localscript to not fire the server

try using findfirstchild instead. Since the sword variable is not a table, using findfirstchild would be better

sword:FindFirstChild("itemsword"):Clone()
1 Like

You can try another method:

equipSword.OnServerEvent:Connect(function(player, itemsword)
local backpack = player.Backpack
local starterGear = player.StarterGear

   if backpack:FindFirstChild(itemsword.Name) or starterGear:FindFirstChild(itemsword.Name) then -- Player has the tool
      return
   else

         local clone = sword:WaitForChild[itemsword]:Clone()
	    clone.Parent = player.Backpack

	    local clone = sword:WaitForChild[itemsword]:Clone()
	    clone.Parent = player.StarterGear

end

Please excuse the spacing.

1 Like

nothing above worked. still not cloning the sword.

Can you show me the code that fires the equipSword remote event?

local replicatedStorage = game:GetService("ReplicatedStorage")

local gearEvent = replicatedStorage.Swords:FindFirstChild("Equip")

local Gear = script.Parent.Parent

local buy = script.Parent

local unequipped = script.Parent.Parent.Unequip

local info = script.Parent.Parent.Frame.info

buy.MouseButton1Click:Connect(function()

local itemsword = Gear.Name

gearEvent:FireServer(itemsword)

info.BackgroundTransparency = 0

info.BackgroundColor3 = Color3.new(1, 0.0588235, 0.0588235)

unequipped.Visible = true

buy.Visible = false

end)

I made a small error, replace this line:

if backpack:FindFirstChild(itemsword.Name) or starterGear:FindFirstChild(itemsword.Name) then -- Player has the tool

with:

if backpack:FindFirstChild(itemsword) or starterGear:FindFirstChild(itemsword) then -- Player has the tool

Then, replace the following lines:

local clone = sword:WaitForChild[itemsword]:Clone() 
clone.Parent = player.Backpack

local clone = sword:WaitForChild[itemsword]:Clone()
clone.Parent = player.StarterGear

with:

local clone1 = sword:FindFirstChild(itemsword):Clone() 
clone1.Parent = player.Backpack

local clone2 = sword:FindFirstChild(itemsword):Clone()
clone2.Parent = player.StarterGear

The issue is that you were setting the parent of the clone to first the player’s backpack then the player’s startergear, so you would have not received it until you reset your player. Let me know if that works.

So. the point is that I want to find all the names of sword (if i have) in the backpack. itemsword (returns to me, the name of the sword I clicked on the button. And I need to check other swords in the backpack. In this template that you’re passing me, I’m just checking the itemsword. and I do not need to check it, because I have already deactivated the button of equip (so I can no longer activate this button and clone this sword)

ex: if i click the equip Rainbow sword (its return to me itemsword = RainbowSword)

so, itemsword is just RainbowSword (I need all names in the backback)

I am not sure if I quite understand, but consider the replacements I listed here as I believe it may be the reason why your code is not working…

local clone1 = sword:FindFirstChild(itemsword):Clone() 
clone1.Parent = player.Backpack

local clone2 = sword:FindFirstChild(itemsword):Clone()
clone2.Parent = player.StarterGear

You can revert to the original loop you had earlier.

is itemsword a string? what is it?

yes. it is a string. Fired when i click the button. Each button fires a string to Equip script. (named itemsword)

so, the script goes in replicated storage e find the tool in a folder with the string in itemsword and clone it

thats why i cant use itemsword to find tools in backpack (bc its return just 1 tool.name)

Could I see the local script that fires the remote?

local replicatedStorage = game:GetService(“ReplicatedStorage”)

local gearEvent = replicatedStorage.Swords:FindFirstChild(“Equip”)

local Gear = script.Parent.Parent

local buy = script.Parent

local unequipped = script.Parent.Parent.Unequip

local info = script.Parent.Parent.Frame.info

buy.MouseButton1Click:Connect(function()

local itemsword = Gear.Name

gearEvent:FireServer(itemsword)

info.BackgroundTransparency = 0

info.BackgroundColor3 = Color3.new(1, 0.0588235, 0.0588235)

unequipped.Visible = true

buy.Visible = false

end)

I found a solution. But ty for helping