chris1384 Posted April 12, 2023 Share Posted April 12, 2023 (edited) It's going to be hard to explain so bear with me. Supposed we have a function like this (function is in a string): exports.myresource:addProperty("security", "function(player) return getPlayerName(player) == 'owner' end") Then, in myresource the string is then put in a loadstring function, as following (it only checks it, doesn't add it anywhere): local testPlayer = getPlayerFromName("player1") -- testing purposes local loadstringed = loadstring(name.. " = ".. func_str) local success, info = pcall(loadstringed, testPlayer) iprint(success, info) -- outputs: true, nil Apparently everything seems to work, the function does indeed exist, but doesn't seem to take any parameters into it. How do you make a function loaded with loadstring() that also supports parameters? Edited April 12, 2023 by chris1384 Link to comment
Moderators IIYAMA Posted April 12, 2023 Moderators Share Posted April 12, 2023 52 minutes ago, chris1384 said: How do you make a function loaded with loadstring() that also supports parameters? When loading code, you are putting a kind of chunk/function around it. So basically this: function(player) return getPlayerName(player) == 'owner' end Looks more or less like this: -- pcall(loadstringed) is calling this chunk/function: function () function(player) return getPlayerName(player) == 'owner' end end So in order to solve this: return function(player) return getPlayerName(player) == 'owner' end local success, theFunc = pcall(loadstringed) local success2, isOwner = pcall(theFunc, testPlayer) See also xpcall for better error handling: https://www.gammon.com.au/scripts/doc.php?lua=xpcall 1 Link to comment
P[ow]er Posted April 13, 2023 Share Posted April 13, 2023 local func_str = "function(player) return getPlayerName(player) == owner end" local loadstringed = loadstring("local owner = 'owner'; return function(player) "..func_str.." end") local myFunc = loadstringed() -- Call the function with a parameter local testPlayer = getPlayerFromName("player1") local result = myFunc(testPlayer) print(result) -- true --- local func_str = "function(player) return getPlayerName(player) == 'owner' end" local loadstringed = loadstring("return "..func_str) local success, myFunc = pcall(loadstringed) -- Call the function with a parameter local testPlayer = getPlayerFromName("player1") local success2, isOwner = pcall(myFunc, testPlayer) print(success2, isOwner) -- true false 1 Link to comment
DiSaMe Posted April 13, 2023 Share Posted April 13, 2023 The proper way in Lua to use arguments in a function created using loadstring is vararg expression, written as three dots. local argument1, argument2, argument3 = ... In your case, the function body would look like this: local player = ... return getPlayerName(player) == 'owner' An example with loadstring: local func_str = "local player = ... return getPlayerName(player) == 'owner'" local loadstringed = loadstring(func_str) local testPlayer = getPlayerFromName("player1") local success, info = pcall(loadstringed, testPlayer) 2 Link to comment
chris1384 Posted April 13, 2023 Author Share Posted April 13, 2023 Everything is working now. It was easier than I thought lol. 11 hours ago, DiSaMe said: The proper way in Lua to use arguments in a function created using loadstring is vararg expression, written as three dots. local argument1, argument2, argument3 = ... In your case, the function body would look like this: local player = ... return getPlayerName(player) == 'owner' An example with loadstring: local func_str = "local player = ... return getPlayerName(player) == 'owner'" local loadstringed = loadstring(func_str) local testPlayer = getPlayerFromName("player1") local success, info = pcall(loadstringed, testPlayer) I was planning to use this in the future, I had to get over the basics first because that's where it screwed me up the most. Thanks for the tip And thank you all for helping! 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