Hi there Dev Forum members,
I’m incredibly perplexed by the logic exampled by the piece of code below.
As is evident, I have defined textArray to include three text values. When I go to reference the value within the for loop below, the first and second iteration run as expected. Yet on the third iteration, textArray returns nil when posRef equals 3
This is an empty script, so no other rogue operations could be interfering with my existing values.
local textArray = {"Hello", "world!", "foobar"}
-- Will iterate with i = 0, 0.4, 0.8 respectively
for i = 0, 1, 0.4 do
-- Converts i to an integer, based on the number of iterations completed
local posRef = (i + 0.4) / 0.4
print("i =", i, ", posRef =", posRef, ", textArray[posRef] =", textArray[posRef])
end
And my output:
i = 0 , posRef = 1 , yet textArray[posRef] = Hello
i = 0.4 , posRef = 2 , yet textArray[posRef] = world!
i = 0.8 , posRef = 3 , yet textArray[posRef] = nil
If anyone has any ideas as to why this is happening, I’d be very grateful to know! ![]()
Thanks!