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

Why won't my sound play? Models and onTouched

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by 5 years ago
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)

Ad

Answer this question