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

Is it possible to play a sound when any parts in a Model is touched?

Asked by 5 years ago

I am just making a game which is all about being the last one standing by hitting each other with objects you will usually see (like a PC, chair, TV,etc.), but I came to think about having sounds on the props.

Yes I can put each part a script connected to their Touched event, but I am sure it'll be VERY slow and hard to update if needed. If I try to make a script that references all parts in the model, it'll work unless it has a different, non-3d item such as a folder.

How would I make a sound play when any part of a model is touched with something? Thanks.

3 answers

Log in to vote
0
Answered by 5 years ago

Here is an answer to your question. Hope it helps!

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
local model = script.Parent
for _,p in pairs(model:GetDescendants()) do
    if p.ClassName == "Part" then
        if p.Touched then
            local sound = game.Workspace.Sound
            sound:Play()
        end
    end
end

If you are not satisfied with that, try this:

local model = script.Parent
local child = model:FindFirstAncestor("TriggerPart")
local boolean = true
function onTouch()
    if boolean == true then
        boolean = false
        local sound = game.Workspace:FindFirstChild("Sound", true)
        sound:Play()
        wait(3)
        boolean = true
    end
end
child.Touched:Connect(onTouch)
Log in to vote
0
Answered by
zblox164 531 Moderation Voter
5 years ago

The simplest way would be this:

Step 1 Put a script in Workspace Step 2 Reference your model and Sound Step 3 Loop around every object in the model Step 4 Check if the parts that are inside of the model were touched. Step 5 If any touch input is detected then play a sound

Final Script:

local Model = workspace.Model
local Sound = workspace.Sound

for _, Parts in pairs(Model:GetChildren()) do
    Parts.Touched:Connect(function()
        Sound:Play() 
    end)
end

Answer this question