Hi everybody. In my game, I have a folder full of IntValues. Each IntValue’s value is given a random number. I want to know how you would check if multiple IntValues share the same value?
I am hoping to have it work like an ‘if’ statement, where if there is multiples of the same value found, it will do x, y, z, etc.
I have been stumped on this for a while and haven’t found any posts that are similar to what I am trying to accomplish. Excuse me for my ‘noobiness’, this is my first forum post. Any help would be appreciated.
There’s a bunch of different ways you can accomplish this, but here’s my recommendation:
Create a table where the key (left side of the table) represents each unique IntValue.Value found within the folder of IntValues, and the value (the right side of the table) has a table which includes every IntValue that had the same Value property as what the key was set to.
I probably explained that in a confusing way, but here’s some examples to make it easier to understand:
Let’s say you have 5 IntValues in the folder and they have the following values:
FirstIntValue: 3
SecondIntValue: 7
ThirdIntValue: 3
FourthIntValue: 1
FifthIntValue: 10
This set of IntValues would be organized in that table in the following way:
local folderOfIntValues = script.Parent.Folder -- Reference where the folder is
local tableOfValues = {
["1"] = {folderOfIntValues.FourthIntValue},
["3"] = {folderOfIntValues.FirstIntValue, folderOfIntValues.ThirdIntValue},
["7"] = {folderOfIntValues.SecondIntValue},
["10"] = {folderOfIntValues.FifthIntValue}
}
As you can see, the IntValues that had the same Value property were placed into the same table. The key (on the left side) represents the number that both of those IntValues share for their Value property.
Here’s some example code of how the script could automatically handle this (if you have any questions, feel free to ask!)
Example Code
local folderOfIntValues = script.Parent.Folder -- Reference where the folder is
local tableOfValues = {}
for iteration, object in folderOfIntValues:GetChildren() do
if not object:IsA("IntValue") then continue end
-- Skips the iteration if an object found within
-- the folder is not an IntValue
local valueProperty = object.Value
local tableCheck = tableOfValues[tostring(valueProperty)]
if not tableCheck then -- If it doesn't find a key in the table with the same value...
tableOfValues[tostring(valueProperty)] = {object}
-- Creates a key in the table that is the same as its "Value" property
-- The value associated with that key is a table that contains the object
elseif tableCheck then -- If a key was found in the table with the same value...
table.insert(tableCheck, object) -- Adds the IntValue to the table
-- Since a key was already found with the same value, we know another
-- IntValue shares the same Value property
end
warn("Completed tableOfValues")
print(tableOfValues)
end
local folder = game.Workspace:WaitForChild("Folder")
function hasDuplicateValues(folder)
local seen = {}
for _, child in ipairs(folder:GetChildren()) do
if child:IsA("IntValue") then
if seen[child.Value] then
print(child.Name..' '..tostring(child.Value))
print(seen[child.Value].Name..' '..tostring(seen[child.Value].Value))
return true
end seen[child.Value] = child
end
end return false
end
print(hasDuplicateValues(folder))
local folder = game.Workspace:WaitForChild("Folder")
function NoDuplicateValues(folder)
local seen = {}
for _, child in ipairs(folder:GetChildren()) do
if child:IsA("IntValue") then
if seen[child.Value] then
child.Value=math.random(1,100)
return false
end seen[child.Value] = child
end
end return true
end
repeat task.wait(0.33)until NoDuplicateValues(folder)