I’ve been trying to figure out how loadstring works from hours and finally realized this entire hour i’ve had the correct code that should’ve done the thing but it just doesn’t work for some reason when it works for other people.
local code:StringValue = piece.Code
local loadedFunction = loadstring(code.Value)
if loadedFunction then
local env_user_function = setfenv(loadedFunction, {
character = character,
pos = NewCharPosition
})
if env_user_function then
env_user_function()
else
warn("Couldn't set environment!")
end
else
print("Error in loadstring.")
end
and the code.Value is:
print(character)
print(pos)
character.PrimaryPart.CFrame = CFrame.new(pos)
this is example code that i made, why doesn’t it work?
it gives error on both prints saying character is nil and pos is also nil
whats the use case? also this should be in #help-and-feedback:scripting-support.
1 Like
Am trying to make it so you can load code from a StringValue in workspace for example, and I want when its loaded with loadstring, I want to give it args/variables from the script
I’ve tried this and it also didn’t work:
(the code.Value)
function PrintString(num)
print(num)
end)
and then call that loadstring in code
local loadstringFunction = loadstring(code.Value)
loadstringFunction("Hello")
It didn’t give any error nor did it output anything
ok but like whats the use for this? why do you need to execute code from a string?
Am making chess like game, and basically instead of making a long and complicated code, I simplified it, by putting the code in StringValues as children of the chess piece.
“Its actually quite simple actually.”
just use modules man!
“Its actually quite simple actually.”
That’s also one of the things I thought but I just don’t see why I need to put more effort and time and make the script/code more complicated, when it can be super simple, only if Roblox’s loadstring
worked.
loadstring()
is absolutely deprecated and underrated in my opinion, when it makes everything much simpler and easier.
Edit: The only problem and issue with loadstring()
is that its not well documented, and no one has given clear guide on how to use it.