I'm working on a gun that makes different GUI outcomes happen when I kill other players. The way I want the script to work seems pretty simple: I have every potential GUI outcome in the PlayerGui, so I put every ScreenGUI name in a table, and give a variable a random value from that table. What I don't know is how to use that string value in a path. Ideally the path would be game.Players.LocalPlayer.PlayerGui.<random string value here>.LocalScript. I would enable the script and make the outcome happen, but I don't know how to put that string value in the path. Is it possible?
I find the loadstring
solution to be very poor. Any eval-like function is inherently dangerous and shouldn't ever be used as a crutch -- in cases like this there are definitely ways to avoid using it, and you should.
In this Question I give a full solution to parsing Lua paths
Please note that that solution is fairly long and also probably very overpowered for your needs here. See below.
However, since you've already acknowledged that there's a particular format you'd like, you could just use that instead.
If the only thing that is different is that <random string value here>
you can just use:
game.Players.LocalPlayer.PlayerGui[someString].LocalScript
Where someString
is the string of that name in that location.
In addition to avoiding loadstring
, this sort of organization will be simpler and much easier to understand.
Perhaps I didn't quite grasp the hierarchy you imagined, but a solution like the previous would be far superior to loadstring
for something so easily accomplishable.
Frankly, it is possible in LocalScripts (if my memory serves right). In server scripts, you have to enable LoadString which Admins will frown at.
-- LocalScript pathname = 'workspace.Bebee2' loadstring('return '..pathname)().Head:destroy()
Otherwise, you SHOULD use an ObjectValue.