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

How do you set a parameter through a function when you want to use pcall?

I'm mostly doing this cause I'm learning pcall right now and I just really want to know the answer


function hello()
	print("Touched")
end
script.Parent.Touched:Connect(hello)

Lets say you want to use a hit parameter. How would you do that in that line of code? I'm not a beginner I'm learning pcall.

1 answer

✓ Accepted answer

pcall allows you to run the lines of code inside it in "Protected mode" so that if there is an error the script doesn't break. pcall holds 2 parameters. The first parameter returns a boolean, You can use this, later on, to check if there are any errors in the code. The second argument returns a variant. If there were any errors in the code you can use print() to show what kind of error it was. Basically, it would show the same error as it would without pcall. A pcall function would look like this:

local success, message = pcall(function()
--lines of code
end)

if success then

else
	print(message)
end

You can also use warn() instead of print if you want to.

Now, the thing is that pcall doesn't re-run the function for you. So what you can do is to call upon a function with pcall.

local function hit(p)
	print(p)
end

script.Parent.MouseClick:Connect(function(player)
local Success, Message = pcall(hit, player)
end)

Normally to pass of a parameter inside the brackets of a function like hit(player) but if you look at the explanation for the pcall you see that it takes a parameter after a function. (I'm sorry if I explained that poorly. English isn't my native language)

1 comment

0

@noammao wow really helped

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