Jump to content

Loadstring not working correctly


Elmatus

Recommended Posts

Ok, thanks in advantage for whoever takes time to answer.

I'm trying to make a a script that loads a txt when it starts up. The script is serverside.

I have a very weird problem. If I just put the lua code like normal it works just fine. But if Put it in a .txt or event a .lua outside meta, it doesnt work!

The purpose of what I want is that admin of the sv can change the file without having to have the FTP of the server.

Here is a snippet of my code: (server-side)

function loadTheCode() 
    file = fileOpen ("editable.lua") 
    size = fileGetSize(file) 
    luaCode = fileRead(file,size) 
    loadstring (luaCode) 
    fileClose(file) 
end 
addEventHandler("onResourceStart",root,loadTheCode) 

The code im trying to load includes some callRemote, getAccountSerialand other stuff.

The thing I dont get is why it works inside the in-meta lua but not with loadstring.

The console doesn't outputs errores nor the debugscript. The event does get triggered. I verified luaCode's length and it matches the lua code in it. Any solutions?

Link to comment
You need () after loadstring() to execute it

What?!

BTW Try like this for me was working:

addEventHandler("onResourceStart", getRootElement(), function() 
    local file fileOpen("editable.lua"); 
    local luaCode = fileRead(file, fileGetSize(file)); 
    loadstring(tostring(luaCode)); 
    fileClose(file); 
end); 

Link to comment
You need () after loadstring() to execute it

What?!

What he said makes more sense than your code..

fileRead returns a String already + you don't execute it aswell.

Elmatus code was like this loadstring(luaCode) and John Smith says he need () after loadstring, but Elmatus already have it...

Link to comment
No, he doesn't. Check the original post.

Change line 5 to the following and you'll be good to go.

loadstring(luaCode)() 

What's the point of this? loadstring(luaCode)() i usually use loadstring(luaCode) and works...

loadstring(luaCode) just loads the code, loadstring(luaCode)() loads it and executes it

Link to comment

I would recommend using pcall to ignore code errors.

function onResourceStart() 
    local file = fileOpen("editable.lua") 
    if(file) then 
        pcall(loadstring( 
                 fileRead(file, fileGetSize(file)) 
            ) 
        ) 
        fileClose(file) 
    end 
end 
addEventHandler("onResourceStart", root, onResourceStart) 

Link to comment
I would recommend using pcall to ignore code errors.
function onResourceStart() 
    local file = fileOpen("editable.lua") 
    if(file) then 
        pcall(loadstring( 
                 fileRead(file, fileGetSize(file)) 
            ) 
        ) 
        fileClose(file) 
    end 
end 
addEventHandler("onResourceStart", root, onResourceStart) 

Why'd you want to ignore code errors?

Link to comment
Why'd you want to ignore code errors?

If there a runtime error in the code you load with loadstring, for example trying to call a function that doesn't exist; then when you call the function returned by loadstring, the script you are running will stop execution (but mta will still call your code functions in the script, like events, binds etc).

That means if you have code like this:

local func = loadstring("outputChatBoxZ('hello, world!')") 
func() 
outputChatBox("After calling loadstring func") 

then the second outputChatBox will not be called.

But if you are using pcall:

local func = loadstring("outputChatBoxZ('hello, world!')") 
pcall(func) 
outputChatBox("After calling loadstring func") 

then the second outputChatBox will be called.

You will get a chance to recover from script error. pcall also returns true or false if the script executed without any errors.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...