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

Why won't this brick play this sound effect?

Asked by
EpicLilC 150
9 years ago

I made a model, with a working speed boost script. I added a sound, and a script under it in the model. The script says..

function onTouch()
    script.Parent:Play()
end
game.Workspace.Model:connect(onTouch)

Why won't the sound effect work? (If it helps to know, I added background music to my game as well. I want this speed boost brick to play this sound when a player touches it)...

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

Your problem is that you're trying to use the Touchedevent with a model. This event is only supported for individual parts. No problem though, we can easily fix this to work with an entire model still.


Here's a little edit to your script that connects the event to every single part in the model:

function onTouch()
    if not script.Parent.IsPlaying then --Checks if it's already playing or not
        script.Parent:Play()
    end
end

for i,v in pairs(game.Workspace.Model:GetChildren()) do --Iterates over every child in the model
    if v:IsA("BasePart") then --Checks if the child is a part
        v.Touched:connect(onTouch) --Connects the event
    end
end

I also fixed your connection. You need to include the event which is Touched , or else you're not connecting the function to anything properly.

Part:connect(onTouch) --This is what yours would have looked like.
Part.Touched:connect(onTouch) --Needs to be set up like this.

And there you go. It's as simple as that. If you have any further problems/questions, please leave a comment below, and I'll see what I can do. Hope I helped :P

Ad

Answer this question