Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

loadstring Erroring out instead of Printing nil?

I want the Script to actually know if the user is not giving a Valid script through the .Chatted Method..

game.Players.ChildAdded:connect(function(Player)
	if Player:IsA("Player") then
		if Player.Name == "Player" then
			Player.Chatted:connect(function(Script)
				local Call = pcall ( loadstring (Script) )
				
				if Call ~= nil then
					print("nil")
				end
			end)
		end
	end
end)

But instead of printing "nil" it shows in the Output:

22:49:52.788 - attempt to call a nil value 22:49:52.789 - Script 'Workspace.Script', Line 5 22:49:52.790 - Stack End

1 answer

✓ Accepted answer

pcall is trying to call a nil value. That is, loadstring(Script) was nil.

You have to check if loadstring(Script) returned a nil value:

local f = loadstring(Script)
if f then
	print("It's a script!")
	pcall( f )
else
	print("It's not a script.")
end

Be wary of things like while true do end crashing the server, or workspace:ClearAllChildren() ruining it.

This discussion is preserved as it was. New answers, comments, and votes are closed.