How do I implement pcall into my script?

I want to make a free model that is an airhorn, you are supposed to activate the tool and hear a sound, but sometimes the sound doesn’t load, can anybody help me please?

-- Do not mess with this script unless you know what you're doing.

local sound = script.Parent.AirhornSound
local volume = 0.125 -- Change this number to how loud you want it to be!
local length = 2 -- Change this to how much you want the player to wait before using the airhorn again!
local debounce = false

script.Parent.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		sound:Play()
		sound.Volume = volume
		wait(length)
		debounce = false
	end
end)

Does the script error if the sound isn’t loaded? If so then literally just pcall the whole thing if you wanted.

local sound = script.Parent.AirhornSound
local volume = 0.125 -- Change this number to how loud you want it to be!
local length = 2 -- Change this to how much you want the player to wait before using the airhorn again!
local debounce = false

script.Parent.Parent.Activated:Connect(function()
    pcall(function()
	    if debounce == false then
    		debounce = true
    		sound:Play()
    		sound.Volume = volume
    		wait(length)
    		debounce = false
    	end
    end)
end)

Whenever the script errors it won’t stop the whole script for that one error and in fact you can go even more in depth and get the error that occurred in the pcall function.

1 Like