FindFirstChild of an object which name has changed

Basically I have this script to change the name of a tool to a letter and a number, but I also want to check if they already have this tool so they can’t get it again until they lose it.
How could I do this?

local RS = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

local letters = {"A","B","C"}
local numbers = {"1","2","3"}
local Mailman = Teams.Mailman
local proximityPrompt = script.Parent
local mail = RS.ClassItems.Tools.Mailman.Mail
proximityPrompt.Triggered:Connect(function(player)
	if player.Team == Mailman then
		local mm = mail:Clone()
		local letter = letters[math.random(1,#letters)]
		local number = numbers[math.random(1,#numbers)]
        --no clue what to do here
		local check = player.Backpack:FindFirstChild(mail) or player.Character:FindFirstChild(mail)
		if not check then
		mm.Name = letter..number
		mm.Parent = player.Backpack
		end
	end
end)

Is it only one tool? Then you can use :FindFirstChildWhichIsA("Tool") to check if the player has any object of this class.

there’s multiple tools in the game, would’ve done that otherwise

I believe this might help you, try checking if the values exist with string.match String Patterns

I’m still a bit of a beginner so this might not be the most efficient method but instead of using FindFirstChild, I would write a loop that individually checks each tool name in the player’s backpack and see if they’re equal to your randomly generated name.


local inbackpack = false
for i, child in ipairs(player.Backpack:GetChildren())do
   if child.Name == (letter..number) then --or whatever the randomly generated name is
       inbackpack = true
   end
end
if inbackpack == true then
   --put the tool in the player's backpack
end

I had to substitute with 3 spaces instead of tab since my tab key is broken

this wouldn’t really work
since the name is randomly generated everytime you can’t just put the random numbers
there’s no way to know unless I individually check for every possible letter and number combination, which would be incredibly inefficient

1 Like

what values are you referring to?

You said it needs to check for existing names, and can use string.match to find the values in sny combination.

You could try using inserting a bool value in the player and set it to true when they acquire the tool

I suppose that could work, seems kind of like a hacky solution though

I could but that’s kind of a waste of resources for just one tool

fair enough, what about inserting something inside the tool that is unique to that tool alone, then looping through the players backpack to see if they already have it. This probably isnt the most efficient way to do things though

I am a little confused here, if you already have the random values for the tool and are checking to see if there is a copy of that tool, can’t you just use those same random values to find it?

honestly the bool is more effective than that, ill probably just end up using the bool due to the fact most of the other solutions mentioned aren’t much better

the name of the tool is randomly generated everytime
i’m looking for the same tool, but when you have multiple of the tool in your inventory they will all have different names.

Just curious why would you do this?

mail system, you deliver mail to houses which have these numbers for mailbox games

so basically the delivery system from work at a pizza place

It is still referring to the old instances (i believe) so I’d try to move the variables down

local RS = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

local letters = {"A","B","C"}
local numbers = {"1","2","3"}
local proximityPrompt = script.Parent

proximityPrompt.Triggered:Connect(function(player)
local Mailman = Teams.Mailman
	if player.Team == Mailman then
		local mm = mail:Clone()
		local letter = letters[math.random(1,#letters)]
		local number = numbers[math.random(1,#numbers)]
        local mail = RS.ClassItems.Tools.Mailman.Mail
		local check = player.Backpack:FindFirstChild(mail) or player.Character:FindFirstChild(mail)
		if not check then
		mm.Name = letter..number
		mm.Parent = player.Backpack
		end
	end
end)

nope, didnt work
honestly I just dont think it’s possible without using a variable or counter of somekind

as the title says…

for _, OBJECTS in pairs(PARENT_OF_THE_OBJECT:GetChildren()) do
	OBJECTS:GetPropertyChangedSignal("Name"):Connect(function()
		
		--Hmmm....
	
	end)
end