for _, v in pairs(plr.Backpack:GetChildren()) do
if v:IsA("Tool") and require(v:WaitForChild("ToolModule")).ItemCategory == ToolModule.ItemCategory then
print("Player already own a tool of that kind")
else
print("Player doesn't own a tool of that kind")
SellectedTool:Clone().Parent = plr.Backpack
end
end
When the player only have one item (Of it same category of not) the script will work fine. But if the player has more than one, it will print multiple times based on the ammount of tools what it should, but it will just ignore the category thing. What should i do?
Where is ToolModule.ItemCategory stored? Specifically the ToolModule part. What it looks like you are doing is determining if the tool’s ToolModule.ItemCategory is equal to any tools ToolModule.ItemCategory.
Maybe a better question is, where is this script located?
When you compare the tool module’s ItemCategory with servers module’s ItemCategory, what would make them the same? If you have multiple items with different categories, some of them might be the same as the server’s, but some of them might be different. It wouldn’t test to see if they already exist, it would just test to see if it is the same as the server’s ItemCategory.
I don’t know if that made any sense, so let me know if you need me to break it down further lol.
What im trying to do is check if the player already has a primary or secondary. If the player has a primary and try to buy a primary don’t let he get it, same for the melee. I’ve been trying to make it for some time but still nothing
First, inside of the ToolModule script on the server, update the ItemCategory variable to be a table. I would also change the name to something like “OwnedCategories” or something similar.
Then, inside of the “CheckScript” on the server, update the code to look more like this:
CheckScript (Server)
for _, v in pairs(plr.Backpack:GetChildren()) do
if v:IsA("Tool") and ToolModule.OwnedCategories[require(v:WaitForChild("ToolModule")).ItemCategory] ~= nil then
print("Player already own a tool of that kind")
else
print("Player doesn't own a tool of that kind")
SellectedTool:Clone().Parent = plr.Backpack
table.insert(ToolModule.OwnedCategories, require(v:WaitForChild("ToolModule")).ItemCategory)
end
end
It appears what you are trying to do is check if the player already has a specific tool of the given category equipped. This is however not what you are actually doing at the moment. Right now you check if each of the items the player has is of that category, and if they aren’t, you give them the tool.
If you think about what you code is actually doing right now, it is pure nonsense.
Instead you need to loop over all of the tools and check if any of them are of the given category, then give the tool if none was found.
Do note, you may also want to check the player’s character for tools, as any tool the player is currently holding will not show up in their Backpack.
local Tools = plr.Backpack:GetChildren()
table.insert(Tools,plr.Character:FindFirstChildOfClass("Tool")) -- Include any tool being held
local ToolFound = false
for _,Tool in Tools do
if Tool:IsA("Tool") and require(Tool.ToolModule).ItemCategory == ToolModule.ItemCategory then
ToolFound = true
break -- A tool has been found, so we don't need to check the rest
end
end
if ToolFound then
print("Player already own a tool of that kind")
else
print("Player doesn't own a tool of that kind")
SellectedTool:Clone().Parent = plr.Backpack
end
Please take some time to consider why the logic of your original code was faulty, it should help you improve as a programmer.