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

How can i make a script that skip error and keep working ?

Asked by 4 years ago

I have a script that teleport player back to spawn (using loadcharacter) and reward 100 cash when a player touch a part This is my script:

local Debounce = false

script.Parent.Touched:connect(function(hit)
    if Debounce == false then
    Debounce = true
    if hit.Parent.Humanoid ~=nil then 
        print("touched")
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
        player:LoadCharacter()
        workspace.MapChanger.Waittime.Value = workspace.MapChanger.Waittime.Value /2    
        wait(5)
        Debounce = false    
        end
    end
end)

sometime when a player touch it, it give me an error saying Humanoid is not a valid player of Accessory then the script stop working... How can i prevent that?

Sorry for my bad english :(

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

To make script skip error and keep working, we use pcall(). For example:

local Succeed, ErrorMessage = pcall(function()
    write("I'm running in pcall() :D") --this will return error because the function have to be print() not write()
end)

if Succeed then
    print("Nothing wrong :D")
else 
    print("It error :(", ErrorMessage)
end

Succeed is a boolean value, if there is no error, it will return true or it will return false if there is error. ErrorMessage is the error message(duh...) that the script return if there is any error.
pcall() is only use for can't handle error like Http request error, Gamepass purchase,... but that is another thing.
Using pcall() too much is not a good habit, it better to fix the error by your self than skip it.

For more information about pcall(), click here.

*Note: I don't have the link to pcall() document so... Find it your self :)

0
My script now print red color error line (without "It error :(") with a green line left to it.....Is that normal? Nguyenlegiahung 1091 — 4y
0
If that happen then you do it right. The green line left to it show is that line print by client or server(green mean server, blue mean client), and the red color error line is the ErrorMessage that you just print. Block_manvn 395 — 4y
0
why it doesn't print without it error :( while i told it to? Nguyenlegiahung 1091 — 4y
Ad
Log in to vote
1
Answered by
RAFA1608 543 Moderation Voter
4 years ago

Hello. First, i will teach you how to check if something exists.

local hum = hit.Parent:FindFirstChild("Humanoid") --If it finds the humanoid, returns the humanoid, if it doesnt, returns nil
if hum then
    Hum exists!
end

Now, pcalls.

local worked, errormessage = pcall(function() --pcalls are functions that returns if it worked and the error message.
    pppprint()
end)
if worked == false then
    print(errormessage) --would print that pppprint() isnt a existant function or something. yeah.
end

Hope that helped!

0
personally I find that this answer is better, because it explains FindFirstChild FlabbyBoiii 81 — 4y
0
i'ma upvote FlabbyBoiii 81 — 4y

Answer this question