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

How to bypass errors of a ranking system not being in the game...?

I am trying to make an admin ranking system of sorts, and I want to know how to make it so if the target is not in the game, it will not error. Here is the code I am currently using that errors when OtherPart is not in the game.

local targets = {workspace.Part, workspace.OtherPart}

for i,target in pairs(targets) do
target:Destroy()
end

Any help?

1 answer

✓ Accepted answer

You can wrap any function in pcall(), a Lua function that effectively "tries" to execute the code within it like Java and JavaScript's try keyword. If you're not concerned about the error message (you probably aren't since you know what it will most likely be), you can just do this:

pcall(function()
    local targets = {workspace.Part,workspace.OtherPart}
    for i = 1,#targets do
	    targets[i]:Destroy()
    end
end)

You can also use if statements to create a more conservative approach that does not require using the pcall function:

local targets = {workspace:FindFirstChild('Part'),workspace:FindFirstChild('OtherPart')}
for i = 1,#targets do
    if targets[i] then targets[i]:Destroy() end
end

1 comment

0

Thank you for the help.

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