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

How Come When I Try To Get a Return From This Pcall Function, Nothing Happens?

I am experimenting with Pcall functions and I want mine to be able to return a different output, a little like how this function does here.

function GiveResult()
	return 1
end

local a = GiveResult()

repeat wait() until a == 1 or a == 2 or a == 3
if a == 1 then
	print("Player 1 wins")
elseif a == 2 then
	print("Player 2 wins")
else
	print("Nobody wins")
end

However, I want to make my game extra protective from exploiters by wrapping my Game script with a Pcall function. When the function fails, I want it to simply clone a new copy of itself and delete the old one to start over. Right now, I am trying to learn the ins and outs of Pcall functions before actually using them. I have tried to do the same thing that I did with the previous function with this Pcall function, but I never get an output. Why is that?

wait(5)
 
function BadPrint()
    return 1
end

success, message = pcall(BadPrint)
if success then
    if success == 1 then print(1) elseif success == 2 then print(2) end
else
	wait(.1)
    	print("An error occurred: "..message)
	local newScript = game.Lighting.Pcall:Clone()
	newScript.Parent = game.Workspace
	newScript.Disabled = false
	script:Destroy()
end

Any help is much appreciated!

1 answer

✓ Accepted answer

success is boolean. message will contain the return value, so change your if success == 1, etc. to if message == 1

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