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

How to properly use PCall in this situation?

Asked by 8 years ago

Hey there,

I'm currently having issues using pcall properly. My loadstring is executing, but without the argument provided, and not functioning properly. Here's what I'm up to:

01function confirmCommand(gameCommand)
02    local success, message = pcall(execCommand(gameCommand)) -- line 32
03    if success then
04    else
05        print("An error occured: " .. message)
06    end
07end
08 
09function execCommand(executedCommand)
10    loadstring(executedCommand)
11end

This is the "secret" script, which is another error I get. It says error loading module yet it still works each time.

1require(game.Workspace.Module)

Yes, it's one line.

Here are the errors I receive each time:

0114:30:21.559 - Requested module experienced an error while loading
0214:30:21.559 - Stack Begin
0314:30:21.559 - Script 'Workspace.secret', Line 1
0414:30:21.560 - Stack End
0514:30:21.560 - Workspace.Module:32: bad argument #1 to 'pcall' (function expected, got no value)
0614:30:21.560 - Stack Begin
0714:30:21.561 - Script 'Workspace.Module', Line 32 - global confirmCommand
0814:30:21.561 - Script 'Workspace.Module', Line 18 - global checkCommand
0914:30:21.561 - Script 'Workspace.Module', Line 76
1014:30:21.562 - Stack End

Does anyone know what I'm doing wrong?

1 answer

Log in to vote
1
Answered by 8 years ago

With pcall the arguments that will be passed to the function needs to be separate.

E.g :-

01function confirmCommand(gameCommand)
02    local success, message = pcall(execCommand, gameCommand) -- function then arguments to be passed
03    if success then
04    else
05        print("An error occured: " .. message)
06    end
07end
08 
09function execCommand(executedCommand)
10    loadstring(executedCommand)
11end

Hope this helps.

Ad

Answer this question