Hi! So I attempted this challenge to make a color-changing part that stores it’s last colors in a table but looks like it doesn’t work.
Code:
local part = workspace.Part
local lastColors = {} --e.g: {[1] = "really blue", [2] = "really green", ...}
while true do
wait(0.5)
local newColor
repeat
newColor = BrickColor.Random()
until not table.find(lastColors, newColor)
part.BrickColor = newColor
lastColors[#lastColors + 1] = newColor
if #lastColors >= 5 then
for i,v in ipairs(lastColors) do
lastColors[i-1] = v
end
lastColors[0] = nil --remove the oldest color
end
end
I’m getting duplicates in the table. It’s something with the ipairs loop probably.
Thanks!
local part = workspace.Part
local lastColors = {}
while true do
wait(0.5)
local newColor
local function pick_color()
-- function to pick a random color
newColor = BrickColor.Random()
if table.find(lastColors, newColor) then
-- search the table to find if it already exists
pick_color() -- call the function again
end
end
pick_color()
part.BrickColor = newColor
table.insert(lastColors, newColor)
-- use table.insert, it's easier
if #lastColors >= 5 then
for i,v in ipairs(lastColors) do
lastColors[i-1] = v
end
lastColors[#lastColors] = nil -- the last value in the table will be a duplicate
lastColors[0] = nil --remove the oldest color
end
end
I would just use table.remove instead of writing your own remove code. I haven’t really analyzed it yet, but I think your issue is a mistake in the removing part.
Oh, alright, thank you, so the problem was that I didn’t do lastColors[#lastColors] = nil.
I added it to my code and now it works perfectly.
local part = workspace.Part
local lastColors = {} --e.g: {[1] = "really blue", [2] = "really green", ...}
while true do
wait(0.5)
local newColor
repeat
newColor = BrickColor.Random()
until not table.find(lastColors, newColor)
part.BrickColor = newColor
lastColors[#lastColors + 1] = newColor
if #lastColors >= 5 then
for i,v in ipairs(lastColors) do
lastColors[i-1] = v
end
lastColors[0] = nil --remove the oldest color
lastColors[#lastColors] = nil
end
print(lastColors)
end
Can you explain me why the last value in the table will be a duplicate? I kinda don’t understand it yet.
The loop only moves the current value to a lower index as long as there is a value above it. Since the last value in the array doesn’t have anything to replace it with, it keeps it’s value and stays in the array thus being a duplicate. My explanation might be a little confusing
The reason why the last value is still very brown is because there isn’t another index to overwrite it, to examine your code:
for i,v in ipairs(lastColors) do
lastColors[i-1] = v
-- when the loop is on the last value
-- nothing happens to it because the loop only affects the previous value, and since it's the last entry
-- it can't be a 'previous value'
end
This either creates a new index or overwrites the previous one. When the loop is on the last entry, it only overwrites the value beneath it and the loop ends because it was the last entry, therefore, that entry itself, isn’t affected because there isn’t another value in front of it to make it become another value. I’m not the best at explanations lol