Hey, i am creating a little game, where you need to click the generated pattern, and i got it nice, but i got a problem; You can’t compare a table with other table, like:
if table == table2 then
and as there is no :GetChildren for tables, i dont know how to do that. here is my code lol:
local buttons = workspace.Colors:GetChildren()
local startButton = workspace.StartButton
local pattern = {}
local customPattern = {}
local timesClicked = 0
local red = workspace.Colors.Red.ClickDetector
local green = workspace.Colors.Green.ClickDetector
local yellow = workspace.Colors.Yellow.ClickDetector
local blue = workspace.Colors.Blue.ClickDetector
local canClick = true
local yTurn = false
red.MouseClick:Connect(function()
if canClick then
table.insert(customPattern, "Red")
timesClicked += 1
print(customPattern)
end
end)
green.MouseClick:Connect(function()
if canClick then
table.insert(customPattern, "Green")
timesClicked += 1
print(customPattern)
end
end)
yellow.MouseClick:Connect(function()
if canClick then
table.insert(customPattern, "Yellow")
timesClicked += 1
print(customPattern)
end
end)
blue.MouseClick:Connect(function()
if canClick then
table.insert(customPattern, "Blue")
timesClicked += 1
print(customPattern)
end
end)
local function checkPatterns()
if timesClicked == #pattern then
canClick = false
if customPattern == pattern then
print("Nice!")
else
print("Wrong!")
end
end
end
local function createHigh()
local val = 1
for i = 1, #pattern do
local high = Instance.new("Highlight")
high.Parent = pattern[val]
high.Adornee = pattern[val]
high.FillTransparency = 0.7
val += 1
wait(1)
high:Destroy()
wait(1)
end
end
local function createPattern()
local randomButton = buttons[math.random(1, #buttons)]
table.insert(pattern, randomButton)
createHigh()
yTurn = true
while timesClicked < #pattern do
if yTurn == true then
canClick = true
end
wait()
print("timesclicked "..timesClicked,"#pattern"..#pattern)
end
end
startButton.ClickDetector.MouseClick:Connect(function()
table.clear(customPattern)
createPattern()
checkPatterns()
print(pattern)
canClick = false
timesClicked = 0
end)
local function tComparison(table1, table2)
for i1, value1 in pairs(table1) do
for i2, value2 in pairs(table2) do
if value1 ~= value2 or i1 ~= i2 then
return false
end
end
end
return true
end
You’re essentially looping thru both tables and you’re comparing each one.
So it goes thru i = 1orthe first value of the first table and it compares all the values of table2 with that value and then it moves on to the next value in the first table and it repeats until it finishes comparing the first table’s last value to all the second table’s values
I know it sounds confusing but that’s the only good way to explain it
local function tComparison(table1, table2)
for index, value in pairs(table1) do
if value ~= table2[index] then
return false
end
end
return true
end
This time I’m just going thru the first table and using the index, I get the value with the same position from the second table and compare them
I just go thru the first table here
This is comparing the current value from the first table “value” and I’m comparing it to the value with the same index
When you use Square Braces next to a table, you’re accessing a value from it… here’s an example:
local table = {
-- value 1
["v1"] = true
-- value 2
["v2"] = false
}
print(table["v1"])
-> true
print(table[1])
-> true
print(table[2])
-> false
when you just straight up use a number, you’re accessing the value that matches up with it
so imagine it like this:
local t = {
[[ 1 ]]
"value"
}
so when you try t[1] it will print value
Each time you add something to the list, it takes the amount of items in the table and adds a one
So if there was 4 items in the table and you added another one: it would add 1 to 4 and that new item would have an index (the number you use to reference it) of 5
sorry for all the yap… I just added an explanation just in case you wanna understand how it works (tried not to overdo it tho)
local function areTablesEqual(t1, t2)
if #t1 ~= #t2 then return false end --make sure arrays are equal length
for i, v in ipairs(t1) do --iterate over first array
if v ~= t2[i] then return false end --compare values
end
return true --arrays are equal
end