Code not working, No errors!

give.Give = function(tool, tablearea, player, equiptable) -- Tool being weapon/armour/potions / Tablearea being Weapons.WeaponTable
	
	for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do  -- Checks all the weapons in the weapons folder
		for serial, name in pairs(tablearea) do -- Checks table
			if item.Name == name["Name"] and name["Owned"] == true then -- if they own it
				 if item.Stats.Serial.Value == serial then -- Checks to see if it's the correct serial
					print(item.Stats.Serial.Value.." "..serial) -- Test to see if they were the same which they are
					if serial == equiptable["Weapon"] then
						local weap = item:Clone()
						weap = player.Character
					end
					if name["Quantity"] >= 2 then
						for i = 1,name["Quantity"] do
							adding.AddedEvent(item, tool, player)
						end
					else
						adding.AddedEvent(item, tool, player)
					end
				end
			end
		end
	end
	
end

So this is the part of the code which checks the items which will get added to the inventory, but for some reason whenever i add the if item.Stats.Serial.Value == serial then the script just doesn’t continue, they both the same, I tested the print function before and it showed 1 1, so I don’t get why it doesn’t work. Table is layed out like this:

local Weapons = {}

Weapons.WeaponsTable = {
	
	[1] = {
		["Name"] = "Wooden Sword",
		["Owned"] = true,
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 1
	},
	
	[2] = {
		["Name"] = "Wooden Sword",
		["Owned"] = false,
		["Damage"] = 7,
		["Magic Damage"] = 4,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 1
	},
	
	[3] = {
		["Name"] = "Wooden Sword",
		["Owned"] = false,
		["Damage"] = 10,
		["Magic Damage"] = 5,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	
	[4] = {
		["Name"] = "Wooden Sword",
		["Owned"] = false,
		["Damage"] = 13,
		["Magic Damage"] = 7,
		["Health"] = 2, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	
	[5] = {
		["Name"] = "Stone Sword",
		["Owned"] = false,
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	-- [2].. [3].. [4].. and so on.
	
}



return Weapons
2 Likes

Are you trying to clone a tool into a players backpack? Because that’s what it looks like lol. The post didn’t really provide context not to sound rude. If you are…

Is this line a different way to set the tools parent to the backpack? If not, I suggest changing it. Also, not sure if you have a characteradded event but try and use that was well. Use characteradded:wait() so it can clone to the players backpack. Assuming that’s the goal.

1 Like

Nothing to with that line just if item.Stats.Serial.Value == serial then because this the only line that doesn’t work, when I remove it, it works fine. but I need this line so it doesn’t give the wrong variant of the sword.

2 Likes

What is the Serial’s ClassName? If the type can’t equate to a number, it won’t pass, but it’ll still print ‘normally’

1 Like

Serial is [1] in the weapon table, that’s what I am checking to see if the value in the weapon is equal to that serial. Is there anyway to get the serial number?

2 Likes

Yes, but the value object in the Stats folder of the weapon, what is it’s class name? StringValue? IntValue?

2 Likes

oh, It’s a string value. Let me change it to intvalue or numbervalue.

2 Likes

It seems to work with an intValue, but now it just duplicates the sword twice.

2 Likes

Do you happen to have multiple weapons with the same serial by accident? Not the WeaponsTable but the actual tool objects.

If not, maybe try using Humanoid:EquipTool over parenting to the character?

1 Like

So I don’t have multiple weapons with same serial. This is the code for adding it to the inventory:

add.AddedEvent = function(item, class, player) -- Starting the game and gaining something like a sword  (class - Weapon/Potion/Armour)
	local items = ReplicatedStorage[class]:GetChildren()
	for i, v in pairs(items) do
		if v.Name == item.Name then	
			for num, rarity in pairs(script.Rarities:GetChildren()) do
				if v.Stats.Rarity.Value == rarity.Name then	
					--print(item.Name, class, player.Name)
					local rare = rarity:Clone()	-- Clone the rarity ui
					local clone = v:Clone() -- Cloning the tool
					clone.Parent = rare.ItemView -- Setting the tool to the viewportFrame
					local bp = v:Clone()
					bp.Parent = player.Backpack
					local Camera = Instance.new("Camera") -- Creating a Camera
				    rare.ItemView.CurrentCamera = Camera -- Setting the viewports CurrentCamera to the new one
					Camera.Parent = rare.ItemView -- The camera parent is the viewportFrame
					Camera.CFrame = CFrame.new(v.Handle.Position + Vector3.new(0, 0, -1), v.Handle.Position)--Camera.CFrame = CFrame.new(Vector3.new(0,3,0), v.Handle.Position)-- Position of the camera
					rare.Parent = player.PlayerGui.Inventory.Inventory[class.."Inv"]
					print("Added "..item.Name.." to your inventory")
				end
			end	
		end
	end
end

This is the script which duplicates it, not sure why.

Edit: I know why and I’m not sure how to fix it. So there is a for i, v in pairs(items) do which would loop each time the fuction is called upon, in this case when it is sent it cycles through it once, and adds all items to my inventory, but since I own two weapons it cycles through it twice. So I need to remove the for loop, but I’m not to sure what to change it to.
Edit: I fixed it, but I marked you other reply as solution since that was the solution to my other problem.

3 Likes