• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua help with my script please

darknelson

Member
Joined
Jun 19, 2011
Messages
190
Solutions
1
Reaction score
15
im trying to use this script for show me txt file on dialog window

but shows me this error

Lua:
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/mounts.lua:eek:nSay
data/talkactions/scripts/mounts.lua:3: attempt to index global 'file' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/talkactions/scripts/mounts.lua:3: in function <data/talkactions/scripts/mounts.lua:1>

Lua:
function onSay(cid, words, param)
file = io.open('mounts.txt', 'r')
notice = file:read(-1)
doShowTextDialog(cid, 7528, notice)
file:close()
end

OTX 3.10
 
Solution
use local variables..
using global variables like you are doing is a bad practice, as you'll eventually over-write information you weren't meaning to when you inevitably use the same variable name a second time.
---
For your issue;
You should check that the file exists before using it.
More then likely mounts.txt does not exist in the file directory you are currently using.
Lua:
function onSay(cid, words, param)
    local file = io.open('mounts.txt', 'r')
    if file ~= nil then
        local notice = file:read(-1)
        doShowTextDialog(cid, 7528, notice)
        file:close()
    else
        print("Unable to find file 'mounts.txt' in the directory you have chosen.")
    end
end
use local variables..
using global variables like you are doing is a bad practice, as you'll eventually over-write information you weren't meaning to when you inevitably use the same variable name a second time.
---
For your issue;
You should check that the file exists before using it.
More then likely mounts.txt does not exist in the file directory you are currently using.
Lua:
function onSay(cid, words, param)
    local file = io.open('mounts.txt', 'r')
    if file ~= nil then
        local notice = file:read(-1)
        doShowTextDialog(cid, 7528, notice)
        file:close()
    else
        print("Unable to find file 'mounts.txt' in the directory you have chosen.")
    end
end
 
Solution
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/mounts.lua:eek:nSay
not enough memory


edit: hey bro fixed it thanks

Lua:
function onSay(cid, words, param)
    local file = io.open('data/talkactions/scripts/mounts.txt', 'r')
    if file ~= nil then
        local notice = file:read("*a")
        doShowTextDialog(cid, 7528, notice)
        file:close()
    else
        print("Unable to find file 'mounts.txt' in the directory you have chosen.")
    end
end
 
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/mounts.lua:eek:nSay
not enough memory


edit: hey bro fixed it thanks

Lua:
function onSay(cid, words, param)
    local file = io.open('data/talkactions/scripts/mounts.txt', 'r')
    if file ~= nil then
        local notice = file:read("*a")
        doShowTextDialog(cid, 7528, notice)
        file:close()
    else
        print("Unable to find file 'mounts.txt' in the directory you have chosen.")
    end
end
oh yeah, forgot to change your -1 xD
my bad
 
I think you can catch the error, it might be other reasons why a file cant be read. (etc permission issues on linux).
Lua:
local file, err = io.open("mounts.txt", "r")
if file then
    -- do stuff, then close the file. 
    io.close(file)
else
    print("Error opening file: " .. err)
end
 
Back
Top