Kasumo
|
  |
| Joined: 22 Mar 2013 |
| Total Posts: 2467 |
|
|
| 07 Dec 2016 07:11 PM |
| does pcall allow the script to continue even if there's an error in a line? |
|
|
| Report Abuse |
|
|
| |
|
Lykaon
|
  |
| Joined: 27 Oct 2014 |
| Total Posts: 784 |
|
|
| 07 Dec 2016 07:18 PM |
its meant for you to handle errors while at the same time prevent the entire script from being rerumued.
Epix Incorporated Agent Lykaon || #game.Players.PlayerAdded:connect(function(plr) plr:Kick() end) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 07 Dec 2016 07:21 PM |
Not really, it just gives you the ability to determine whether a function was successful or not. If it ends in error, its first argument will be false.
For example, a random situation...
for i = 1,5 do local part = Instance.new("Part",workspace) part.Name = i end -- we have 5 parts named 1-5 in the workspace
function Func() local pick = math.random(1,10) workspace.Pick.BrickColor = BrickColor.random() end
while wait(1) do success, message = pcall(Func) if success then print("Success!") else print("Failure!",message) end end
|
|
|
| Report Abuse |
|
|
Lykaon
|
  |
| Joined: 27 Oct 2014 |
| Total Posts: 784 |
|
|
| 07 Dec 2016 07:22 PM |
^ i think thats what i meant by "handle errors" but ok.
i see how it is. :(
Epix Incorporated Agent Lykaon || #game.Players.PlayerAdded:connect(function(plr) plr:Kick() end) |
|
|
| Report Abuse |
|
|
Kasumo
|
  |
| Joined: 22 Mar 2013 |
| Total Posts: 2467 |
|
|
| 07 Dec 2016 07:26 PM |
| but if a function isn't successful won't you just get output |
|
|
| Report Abuse |
|
|
Lykaon
|
  |
| Joined: 27 Oct 2014 |
| Total Posts: 784 |
|
|
| 07 Dec 2016 07:29 PM |
well if it isnt successful, it just errors, but pcall prevents an entire script from being pooped on
Epix Incorporated Agent Lykaon || #game.Players.PlayerAdded:connect(function(plr) plr:Kick() end) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 07 Dec 2016 07:37 PM |
No, pcall returns whether a script errored or not. It does not protect the function.
However, to leverage pcall to protect your function (for stuff like data storing) you would put that pcall in a LOOP, and then run the function over and over until pcall returns true (i.e. the function was successful)
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 07 Dec 2016 07:38 PM |
oops *pcall returns whether a -FUNCTION- errored or not.
|
|
|
| Report Abuse |
|
|
i_Ja
|
  |
| Joined: 06 Apr 2014 |
| Total Posts: 9297 |
|
|
| 07 Dec 2016 07:56 PM |
pcall stops the script from breaking. pcall lets you print the error instead of making the entire script break.
~Add 5k+ Posts~ {Previous Names: ContentCrown, i_Movie, SurfaceLight} |
|
|
| Report Abuse |
|
|