Loadstring() not working properly?

I’m trying to make an SB system with loadstring() and vLua, I’ve figured out how to get localscripts working with vLua, but loadstring() is erroring for some odd reason.

Error:

Workspace.Script:11: attempt to call a nil value - 48

Code (Serverside):

function LoadSS(player, _script)
	local newVariables = {
		["NLS"] = "function(str) local str = str..'local owner = game.Players[\'"..player.Name.."\'] ' print(str) game.ReplicatedStorage.LoadLocal:FireClient(owner, str) end ";
	}
	
	local NS = "local owner = game.Players['"..player.Name.."'] "
	for name, code in pairs(newVariables) do
		NS = NS .. "local "..name .." = "..code .. " "
	end
	NS ..= _script
	print(NS)
	local Successful, Response = pcall(function() local s = loadstring(NS)() return s end)
	return Response
end

-- This code is just an example that I've written a while ago for VSB

wait(5)

local a = LoadSS(game.Players:WaitForChild('infiniteRaymond'), [[
local p = owner
local char = p.Character
local p = Instance.new('Part', char)
p.Name = "CameraPart"
p.Anchored = true
p.CanCollide = false
local m = Instance.new('SpecialMesh', p)
m.MeshId = "rbxassetid://2540333271"
m.TextureId = "rbxassetid://2540333290"
m.Scale = Vector3.new(1,1,1)
local remote = Instance.new('RemoteEvent', char)
remote.Name = "CameraRemote"
remote.OnServerEvent:Connect(function(player, cf)
	p.CFrame = cf * CFrame.Angles(0, math.rad(180), 0)
end)
NLS("
local p = game.Players.LocalPlayer
local char = p.Character

local remote = char.CameraRemote
local part = char.CameraPart

part.Transparency = 1

game:GetService('RunService').Heartbeat:Connect(function()
	remote:FireServer(workspace.CurrentCamera:GetRenderCFrame())
end)
", char)
]])

print(a)

It prints the script with the NLS and owner code at the top, but still errors none-the-less.
Am I using loadstring() wrong?

Has loadstring been enabled? It’s the LoadStringEnabled property of ServerScriptService.

image

Do you know which line is line 11?

Its this line:

I believe this is coming from the NLS function, as removing the code that runs the NLS function makes it not error.

But I also have a problem where it doesn’t catch output, but I can fix that easily.

It’s saying you’re attempting to call a nil value, so that might mean that pcall is nil. Are you sure that’s the line that’s erroring?

Yes, it’s the line that’s erroring.

Then that’s the only possible issue that I can think of.

Well, when I run the NLS code, it works perfectly fine:

local owner = game.Players:WaitForChild('infiniteRaymond')
wait(2)
local NLS = function(str)
	local str = str..'local owner = game.Players["infiniteRaymond"] '
	print(str)
	game.ReplicatedStorage.LoadLocal:FireClient(owner, str)
end 
NLS("print('test')")

Edit: OK, I’m just confused.

I don’t know if it’s the SB or the script i’m using to test, but other things with the NLS function seem to work fine…

1 Like