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

How to make a brick with sound?

Asked by 8 years ago

I need a script or I need help how to make a script and a brick with sound so like if a object in the game hits the brick it makes that sound... how to do that?

2 answers

Log in to vote
2
Answered by
Master_JJ 229 Moderation Voter
8 years ago

The code below is how to do so, explanations will come after I type the code.

local part = script.Parent
local sound = part:WaitForChild("Fire")
local debounce = false


part.Touched:connect(function(part)
    if part and part.Parent:FindFirstChild("Humanoid") then
        if debounce == false then
            debounce = true
            sound:Play()
            wait(sound.TimeLength)
            debounce = false
        end
    end
end)

The code above first states 3 variables: part, sound, and debounce. Part is the parent of the script, which is the part the player touches. Sound is the sound that is going to be played. Put the sound inside the part. For sound, it says "part:WaitForChild("Fire")" Change "Fire" with the name of the part. For example, if the sound was named "Fly" then it would be "part:WaitForChild("Fly")". The variable debounce is a Boolean value(values that are set to true or false), this will be used in the function.

On the 6th line it says part.Touched:connect(function(part). This means the function will start when the part is touched. We then check to make sure it only works if a player touches it. We do this by saying "if part and part.Parent:FindFirstChild("Humanoid") then". We then use the debounce, to make sure the same sound will not play 2 times, at the same time. By saying "sound:Play()" we are playing the sound. We then say "wait(sound.TimeLength)", followed by "debounce = false". What this does it wait until the sound is finished playing, then allows it to be played again.

Hope this helped :)

Ad
Log in to vote
0
Answered by 8 years ago

http://wiki.roblox.com/index.php?title=API:Class/BasePart/Touched

This will help you learn.

0
Comment much? User#11440 120 — 8y

Answer this question