Page 1 of 1
Why is this script crashing?
Posted: Tue Dec 09, 2014 12:43 am
by GoldenShadowGS
Code: Select all
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
comb[count] = i
local order = table.concat(comb)
local s, b = string.find(combination, order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
end
end
end
--end
end
This is the combination script I just wrote for the help thread. If I don't use solved variable to prevent the function from working, the second time you try to enter the combination, the script crashes with this error:
Code: Select all
local order = table.concat(comb) -- invalid value (nil) at index 9 in table for 'concat'
Is this a bug?
Re: Why is this script crashing?
Posted: Tue Dec 09, 2014 1:39 am
by Batty
With comb[r] = nil you place nil in the comb table so maybe do comb[r] = 0 instead?
Re: Why is this script crashing?
Posted: Tue Dec 09, 2014 2:35 am
by GoldenShadowGS
Why does it always crash at index 9 on the 2nd attempt at solving the combination? I can't figure it out. If nil was causing a crash, it would happen on the first click.
Re: Why is this script crashing?
Posted: Tue Dec 09, 2014 3:27 am
by GoldenShadowGS
I figured out what was going on. An extra index was being added after the puzzle was solved so I have a logic check so the script stops if the count is greater than the combination length
Code: Select all
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
print(count)
print(string.len(combination))
if count <= string.len(combination) then
comb[count] = i
local order = table.concat(comb)
local s, b = string.find(combination, order)
print(s,b)
print(order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
dungeon_door_portcullis_1.door:close()
end
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
dungeon_door_portcullis_1.door:close()
end
end
end
--end
end
Alternate method:
Code: Select all
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
comb[count] = i
local fullorder = table.concat(comb)
local order = string.sub(fullorder,1,count)
local s, b = string.find(combination, order)
print(s,b)
print(fullorder)
print(order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) + 1 do
comb[r] = 0
end
count = 0
dungeon_door_portcullis_1.door:close()
end
end
end
--end
end
Re: Why is this script crashing?
Posted: Tue Dec 09, 2014 5:42 am
by minmay
GoldenShadowGS wrote:Why does it always crash at index 9 on the 2nd attempt at solving the combination? I can't figure it out. If nil was causing a crash, it would happen on the first click.
table.concat goes up to the highest integer key; otherwise,how would it know when to stop? As you can see in the
Lua reference manual, it requires that all elements be numbers or strings. So if there's a nil value in the middle of the "array" you'll get an error, the same as you would if you had a function or table in the middle of the table.