Hello There!
I want to check every string in table with “if” statement.
I want to make that when the script finds “Rarity”, then it does not execute the code
First of all I make function that it add everything in table
In print it looks like this:
function AutoDelete:CheckVariables(Player)
if Player.Character then
local AutoDeleteFolder = Player:WaitForChild("AutoDeleteFolder")
local AutoDeleteVariables = {}
for _,v in pairs(AutoDeleteFolder:GetChildren()) do
if v.Value == true then
table.insert(AutoDeleteVariables, v)
end
end
return AutoDeleteVariables
end
end
then I make Main Script where I want to check it
local PetChoosedRandom = self:ChooseRandomPet(Player, Egg)
for i,v in pairs(Pets) do
if v.Name == PetChoosedRandom then
-- Here Checking Variable and if not find PetRarity Name from Auto-Delete not executing it.
if AutoDeleteModule:CheckVariables(Player) ~= PetRarity.Value then
local Clone = Pet:Clone()
Clone.Parent = Player.PlayerPets
end
end
end
You can execute a function when a string is not found in a table with something like this:
local tableOfStrings = {
"Common",
"Rare",
"Legendary"
}
local function FunctionToExecute()
print("The function executed")
end
local function ExecuteIfNotFound(stringToLookFor)
for index, value in ipairs(tableOfStrings) do
if value == stringToLookFor then
return --exit the function if it found it
end
end
FunctionToExecute() --this will get executed if the string is not found
end
ExecuteIfNotFound("something that doesn't exist") --> will print "The function executed"
ExecuteIfNotFound("Rare") --> will not print anything
If you want to check if PetRarity.Value is not present inside the table returned by CheckVariables(), you can use a function called table.find which searches the table for a specific value, like this:
local PetChoosedRandom = self:ChooseRandomPet(Player, Egg)
for i,v in pairs(Pets) do
if v.Name == PetChoosedRandom then
if not table.find(AutoDeleteModule:CheckVariables(Player), PetRarity.Value) then
local Clone = Pet:Clone()
Clone.Parent = Player.PlayerPets
end
end
end
Also I assume the values inserted into AutoDeleteVariables are instances with the names of the rarity values? So you’ll have to insert their names instead of the instances themselves for it to work:
function AutoDelete:CheckVariables(Player)
if Player.Character then
local AutoDeleteFolder = Player:WaitForChild("AutoDeleteFolder")
local AutoDeleteVariables = {}
for _,v in pairs(AutoDeleteFolder:GetChildren()) do
if v.Value == true then
table.insert(AutoDeleteVariables, v.Name) --insert the name
end
end
return AutoDeleteVariables
end
end
Also you might want to implement some error handling for when the player has no character and CheckVariables doesn’t return anything