Need help with loadstring()

Hi everyone

I’m the co-owner of a roblox pos company called MetroSolutions. most of you probaly dont know this, however roblox has recently started taking down obfuscated modulescripts, and since we dont want this happening, we are moving over to loadstring.

Now, the issue I encountered is, that it appears to not be able to do anything more than print(). I tried it with setting a TextLabel’s text, but it just ended up doing nothing, and not even displaying an error message.

I use JSON for this and this is what the code for retrieving the code to execute it looks like

local HTTPService = game:GetService("HttpService")
local HTTPRequest = HTTPService:GetAsync("https://hauber.tk/ms-licensing/source/main.json")
local HTTPResponse = HTTPService:JSONDecode(HTTPRequest)
local ExecuteCodeFromRequest = loadstring(HTTPResponse["sco"].code)

print(HTTPResponse["sco"].code)
print("executing")
ExecuteCodeFromRequest()

This is the JSON that its retrieving

{
  "sco": {
    "code": "local PlayerService = game:GetService(\"Players\") PlayerService.PlayerAdded:Connect(function(plr)\t local playerGui = PlayerService.PlayerGui\t playerGui.ScreenGui.TextLabel.Text = \"changed from loadstring\"\t print(\"text changed\") end)"
  }
}

(This is all accordingly formatted aswell)

And this is what its outputting in the console (not automatically but because i made it print)
grafik

I really don’t understand why it isnt running, can anyone help me on this?

P.S: I have already tried running it without the PlayerService thing, but it just gave me a nil error

1 Like

I assume what is happening here is that loadstring doesn’t start working before a player joins because some time has to pass before HTTP request gets the script as a response.

Even if it ran before a player joined, it still wouldn’t work because PlayerGui is a child of the Player, not the Players (or PlayersService in your code). You need to get it from the newly joined player itself:

local playerGui = plr.PlayerGui

On top of that, your code still might not work because player’s GUI might not be loaded when the event handler runs, you might want replace direct indexings with WaitForChilds until it works.

Oh wow, I didn’t even realize I was calling PlayerGui from PlayerService

Well anyways, I changed up the code a bit but it still doesnt work. Any ideas?

Code:

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(plr)
	local playerGui = plr:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
	playerGui.TextLabel.Text = "changed from loadstring"
	print("text changed")
end)

JSON:

{
  "sco": {
    "code": "local PlayerService = game:GetService(\"Players\")PlayerService.PlayerAdded:Connect(function(plr)\tlocal playerGui = plr:WaitForChild(\"PlayerGui\")\tplayerGui.ScreenGui.TextLabel.Text = \"changed from loadstring\"\tprint(\"text changed\")end)"
  }
}

I did mention one other problem at the very beginning my post, though I forgot to give a solution:

[quote=“BenMactavsin, post:2, topic:2433464,
username:BenMactavsin”]
I assume what is happening here is that loadstring doesn’t start working before a player joins because some time has to pass before HTTP request gets the script as a response.
[/quote]

This one could be fixed by running the same function for existing players:

local PlayerService = game:GetService("Players")

local function playerAdded(plr)
	-- Existing code that needs to be run 
	-- for new players needs to go here.
end

PlayerService.PlayerAdded:Connect(playerAdded)

for _, plr in PlayerService:GetPlayers() do
	playerAdded(plr)
end

Now its throwing the aforementioned nil error

grafik

Current code:

local PlayerService = game:GetService("Players")

local function playerAdded(plr : Player)
	local playerGui = plr:WaitForChild("PlayerGui")
	playerGui.ScreenGui.TextLabel.Text = "changed from loadstring"
	print("text changed")
end


PlayerService.PlayerAdded:Connect(playerAdded)

for _, plr in PlayerService:GetPlayers() do
	playerAdded(plr)
end

I still need assistance with this. Anyone able to help me?