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