Elmatus Posted December 25, 2015 Share Posted December 25, 2015 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
Bonsai Posted December 25, 2015 Share Posted December 25, 2015 I think you have to use the pcall function to execute the loaded string. pcall(luaCode) EDIT: Not the string from fileRead, but from loadString. Link to comment
Elmatus Posted December 25, 2015 Author Share Posted December 25, 2015 Tried that to pcall (loadstring(luaCode)) But nothing Link to comment
GTX Posted December 26, 2015 Share Posted December 26, 2015 I think it's because of callRemote. When callRemote returns back to MTA, I guess data gets lost somewhere. It would be useful if we full code. Also, callRemote requires rights to execute it. Link to comment
John Smith Posted December 26, 2015 Share Posted December 26, 2015 it doesn't load because you're using loadstring(...) instead of loadstring(...) () You need () after loadstring() to execute it (didn't really read the topic, just saw the code above, so sry if it doesn't work, but it should) Link to comment
Tekken Posted December 27, 2015 Share Posted December 27, 2015 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
Bonsai Posted December 27, 2015 Share Posted December 27, 2015 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. Link to comment
Tekken Posted December 28, 2015 Share Posted December 28, 2015 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
Noki Posted December 28, 2015 Share Posted December 28, 2015 No, he doesn't. Check the original post. Change line 5 to the following and you'll be good to go. loadstring(luaCode)() Link to comment
Tekken Posted December 28, 2015 Share Posted December 28, 2015 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... Link to comment
John Smith Posted December 28, 2015 Share Posted December 28, 2015 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
Noki Posted December 28, 2015 Share Posted December 28, 2015 You're also able to pass parameters through the second set of parentheses. Link to comment
Tekken Posted December 29, 2015 Share Posted December 29, 2015 loadstring(luaCode) just loads the code, loadstring(luaCode)() loads it and executes it oou, didn't know that. Link to comment
Elmatus Posted January 5, 2016 Author Share Posted January 5, 2016 Didn't know aswell That worked just perfect, thanks! Link to comment
RenanPG Posted January 5, 2016 Share Posted January 5, 2016 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
John Smith Posted January 5, 2016 Share Posted January 5, 2016 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
arezu Posted January 5, 2016 Share Posted January 5, 2016 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
RenanPG Posted January 5, 2016 Share Posted January 5, 2016 @John Smith As arezu said, when you have something invalid in the code it would break the execution of the main script, and it would stop in the part the error occurs. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now