Pcall missing argument 1

I am trying to use pcall to prevent my code from breaking when a player leaves mid match.

pcall(runGame())

However, after runGame() completes without any errors, I get the error message missing arguemnt 1 at the line I pcall. My research showed that pcall takes a function as argument 1 so I don’t get it.

A pcall runs from its function, so you’ll need to connect it to its function and then run your main one.

local success, result

success, result = pcall(function()
    runGame()
end)

if success then
   print(“Success”)
else
   print(tostring(result))
end
1 Like

While the code provided above by @Schedency will work, it creates a new seperate function to run the pcall, you shouldn’t use pcall this way unless you need a seperate function, a better way to do it is as follows, as pcall calls the variable passed to it as a function, just provide it with the function name

local function runGame()
	--Function code
end

local success,returnval = pcall(runGame)
if success then
	print("Code Run Successfully!")
else
	warn(string.format("Error when executing code!\nError: %s",returnval))
	--Code to handle errors here
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.