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

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

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

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)

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

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