Execute code from a webserver

Currently I am trying to execute code I have stored on a webserver.
I can successfully print out the code and it all looks okay, but when I try run it using loadstring it gives me the following error
image

I have LoadstringEnabled set to true and the scripts RunContext on Legacy.

Here’s an example codeblock of what I am doing just with a swapped uri

local code = game:GetService("HttpService"):GetAsync("https://example.com/script/raw.txt")
local func, err = loadstring(code)

if code == nil then
	print(err)
else
	func()
end

try printing code first, and also try decoding it if its JSON

It prints out successfully and isnt json encoded

The reason you are getting this error is because the returned code to the loadstring is not a function. Instead it is a bunch of code that has already been executed.

How would I go about having this code executed in game? What’s being passed through to me appears to be a string value. So I don’t see how this could have been executed in game already.

i think loadstring returns evaluation result.
from your error which is attempt to call a nil value, i can tell your code from your webserver doesnt return anything / returns nil

local code = game:GetService("HttpService"):GetAsync("https://example.com/script/raw.txt")
local res, err = pcall(function()
	return loadstring(code)
end)

if err then
	print(err)
else
	print("RESULT", res)
end

so is your issue solved?


Seems not I get this error

1: Expected identifier when parsing expression, got Unicode character U+feff

in which line?


Doesn’t specify as it loads in the script as a single line

that means your loadstring received an invalid lua code, try opening your url in a browser and see if it’s really a valid lua code

It seems to be valid lua code when I had a look just now. It also works in game outside of the loadstring.

can you send your current code and your code from the web server?

Sure thing.

GameScript:

local code = game:GetService("HttpService"):GetAsync("https://example.com/scripts/bans/raw.txt")
local load, err = loadstring(code)

if load ~= nil then
	load()
else
	print(err)
end

raw:

local ban_script = game:GetService('HttpService'):GetAsync('https://example.com/scripts/bans/subscript.txt') 
local code = loadstring(ban_script)
code()

subscript:

-- | Connections
local HTTPService = game:GetService("HttpService")
local Players = game:GetService("Players")

-- | Variables
local banAPI = "https://api.example.com/IsUserBanned.ashx/"
local ConsolePrefix = "Example Bans: "

-- | Connections
Players.PlayerAdded:Connect(function(player)
	local response = HTTPService:GetAsync(banAPI..player.UserId)
	local deserialized = HTTPService:JSONDecode(response)
	if not response then
		player:Kick(ConsolePrefix.."Failed to connect to BanAPI")
	end
	if deserialized.Status ~= "OK" then
		player:Kick(ConsolePrefix..deserialized.Status)
	end
	if deserialized.Banned == true then
		player:Kick(ConsolePrefix..deserialized.BanReason)
	end
end)

would you mind removing the url? since you removed it on the previous one

If you are wondering why there is 2 scripts on the webserver, this is because I will be hiding the 2nd one by obfuscating the first webserver script so people cant access the second one.

Ok, just removed them now. Anything else?

can you screenshot your output on the error?

local code = game:GetService("HttpService"):GetAsync("https://example.com/scripts/bans/raw.txt")
print("CODE", code)
local load, err = loadstring(code)

if load ~= nil then
	load()
else
	print(err)
end

then screenshot the output (reminder to censor your url)