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

Why won't my sound play? Models and onTouched

The hierarchy goes

-Model
	-Sound
		-This Script
	-Part
	-Part
	-Part
	-more parts

The script should make the sound play when you touch any of the parts in the model.

function onTouched(Part)
	if Part.Parent.Parent:FindFirstChild("Humanoid") then
		script.Parent.TimePosition = 0
		script.Parent.Playing = true
	end
end

for i, v in pairs(script.Parent.Parent:GetChildren()) do
	if v:IsA('Part') then
		v.Touched:connect(onTouched)
	end
end

1 answer

✓ Accepted answer
local model  = script.Parent.Parent
local sound = script.Parent

local function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		sound.TimePosition = 0
		sound.Playing = true
	end
end

for i, v in pairs(model:GetChildren()) do
    if v:IsA('Part') then
        v.Touched:Connect(onTouched)
    end
end

Two things to fix:

  1. You defined "Part" in the onTouched event as the "Model", however, the "Part" is actually the part which touches one of the model's parts
  2. connect is deprecated, although it sometimes works, use Connect (capitalized)

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