How can i make it only run only once?

There is the code.

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?

2 Likes

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?

That script is at ServerScriptService. About the module, everysingle tool in the game has that module with some other things:

local module = {
	
	ShowOnShop = true;
	Cost = 4;
	Description = "..";
	ItemCategory = "Primary";
	ItemImage = 14628422537;
	
}

return module

1 Like

So how are we getting the ToolModule variable in the ServerScript?

no, all the tools have the one module inside it. The shop one is located at serverscript

2 Likes

Oh, I see.

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.

1 Like

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

2 Likes

What I would do then is adjust a few things.

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.

ToolModule (Server)

local module = {
	
	ShowOnShop = true;
	Cost = 4;
	Description = "..";
	OwnedCategories = {};
	ItemImage = 14628422537;
	
}

return module

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

Let me know if this works.

Try to use break? I may be wrong though, tell me if it helped.

1 Like

That was the first thing i tried

1 Like


didn’t worked very well.
Just noticed that the module doesn’t say if the weapon is a primary or a secondary

1 Like

Instead of a module having the values of the tool, I think it would be better to just use Attributes.

1 Like

Attributes could work, however, they are not as secure as modules. Modules are also much easier to replicate and edit when creating new items.

Well, I wasn’t talking about security, now, lol.

1 Like

I will work on a fix for you. I will have to recreate the problem and then find a solution lol. I’ll get back to you when I am done.

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.

I hope this helps!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.