String.find refuses to works

hi, so I have a script which uses script.find to search players inventory and character for a specific tool however, even tho the script refuses to return true

After debugging it turns out that the tool which string.find is seraching for is indeed included in the for loop

local TriggerPart = script.Parent.TriggerPart.ProximityPrompt
local ServerStorage = game:GetService("ServerStorage")
local NotificationEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SendNotificationEvent")


local function CheckPlayerInventory(player)
	local playerHasAnItem = false

	-- Check the player's Backpack
	for _, item in pairs(player.Backpack:GetChildren()) do
		print("Checking Backpack: " .. item.Name)
		if string.find(item.Name, "SCP-2923") then
			warn("Player already has SCP-2923 in Backpack: " .. item.Name)
			playerHasAnItem = true
			break -- Exit loop early if the item is found
		end
	end

	-- Only check the Character if the item wasn't found in the Backpack
	if not playerHasAnItem then
		for _, item in pairs(player.Character:GetChildren()) do
			print("Checking Character: " .. item.Name)
			if string.find(item.Name, "SCP-2923") then
				warn("Player already has SCP-2923 equipped: " .. item.Name)
				playerHasAnItem = true
				break -- Exit loop early if the item is found
			end
		end
	end

	-- Log final status
	if playerHasAnItem then
		print("Player item detected")
		return true
	else
		print("Player doesn't have item")
		return false
	end
end

TriggerPart.Triggered:Connect(function(player)
	local PlayerHasAnItem = CheckPlayerInventory(player)
	warn(tostring(PlayerHasAnItem))
	if PlayerHasAnItem == true then
		NotificationEvent:FireClient(player, "Action Prohibited", "You already have SCP-2923", 3)
	else
		NotificationEvent:FireClient(player, "Notification", "You picked up a strange Protein Bar", 3)
	end
end)

Here is an output from line 12 proving that an item with said name does exist.

My question is why is the script returning false?
image

Edit: Fixed formatting of the message

1 Like

Bro literally just use findfirstchild with the name of the item, why do you need a pairs loop

I don’t think findfirstchild works with string.find and I have multiple items. SCP-2923-01,-02,-03 etc and I don’t want to list all of them

what does the print say for item name

Exactly the same
image

Well i suggest calling the find function directly from the string itself like

item.Name:find("SCP-2923")

see if that can make it work

1 Like

Didn’t even know there is a :find(), good to know however it failed as well.
image

Part I edited

if playerHasAnItem == false then
		for _, item in pairs(player.Character:GetChildren()) do
			print("Checking Character: " .. item.Name)
			if item.Name:find("SCP-2923") then
				warn("Player already has SCP-2923 equipped")
				playerHasAnItem = true
				break 
			end
		end
	end

string.find is technically faster. Anyways, use the plain argument since - is a special reserved character.

-- https://create.roblox.com/docs/reference/engine/libraries/string#find
-- string, pattern, init (default 1), plain (default false)
string.find(item.Name, "SCP-2923", 1, true)
1 Like

im sorry what the fak is a plain argument my g?

It helps to read the link I provided.

Ok my glorious black king, reading that now.

1 Like

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