Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

loadstring Erroring out instead of Printing nil?

Asked by 8 years ago

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

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

Ad

Answer this question