Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this code always find a matching item in the players inventory?

Ok, for 1 sorry for not a very descriptive title. But basically im trying to match a plays inventory to a list of prohibited items, so a players inventory can be scanned. And this is what ive dont so far.

local Player = Players:GetPlayerFromCharacter(hit.Parent)
local Inventory = Player.Backpack
local ItemsInInventory = Inventory:GetChildren()
local prohibitedItems = {game.ServerStorage.ProhibitedItems["SR-60 Default"], game.ServerStorage.ProhibitedItems.CombatKnife}		


for i, v in pairs(ItemsInInventory)do
			
			for I,V in pairs(prohibitedItems) do 
			
				if v.Name == V.Name then
					print("IllegalItemFound")
					IllegalItemFound = "true" 
					
				end
			
			end
			
		end

However whenever I run this, even if there are no items at all in a players inventory the game will always print "IllegalItemsFound." Yea im not sure whats going on here because ive used a similar scipt in another project and it worked fine there. (fyi, I do have this code wrapped inside a function so I can call it when I need to)

2 comments

0

might be because v.name = v.name you might want to reference the actual item in the table either using the string name v.name == prohibitedItems["SR-60 Default"]

0

yeah, try renaming the for loop for retrieving inventory items to something else like "Inv_Items"

1 answer

Try renaming the for loop for retrieving inventory items, as valk_3D said.

local Player = Players:GetPlayerFromCharacter(hit.Parent)
local Inventory = Player.Backpack
local ItemsInInventory = Inventory:GetChildren()
local prohibitedItems = {game.ServerStorage.ProhibitedItems["SR-60 Default"], game.ServerStorage.ProhibitedItems.CombatKnife}       


for i, v in pairs(ItemsInInventory)do
	for a,g in pairs(prohibitedItems) do 
		if v.Name == g.Name then
			print("IllegalItemFound")
			IllegalItemFound = true
		end
	end
end

This seemed to work for me

1 comment

0

Sorry for responding so late, but renaming the loop did not work. Any other ideas?

This discussion is preserved as it was. New answers, comments, and votes are closed.