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)
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
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.
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)