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

Local sound on touched event not working?

Asked by
TudiPYM 12
3 years ago
local part = script.Parent
local isplaying = false
local soundService = game:GetService("SoundService")
local plr = game.Players.LocalPlayer

local function playLocalSound(soundId)
    if isplaying then 
        return
    end
    isplaying = true
    local sound = soundService.Sound
    soundService:PlayLocalSound(sound)
    sound.Ended:Wait()
    isplaying = false
end

local function onTouch(part)
    playLocalSound(6519564325)
end

part.Touched:Connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")

    if h then 
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if player.leaderstats.Grades.Value > 0 then
                local selling = player.leaderstats.Grades
                local getting = player.leaderstats.Money

                getting.Value = getting.Value + selling.Value
                selling.Value = 0
            end
        end
    end
end)

part.Touched:Connect(onTouch)

Tried to add the sound to a touched event, but the sound just doenst play at all. The output says that the function was triggered tho.

1 answer

Log in to vote
0
Answered by
ud2v3cf 35
3 years ago

The sound does play, however you forgot to set the sound Instance's SoundId.

local part = script.Parent
local isplaying = false
local soundService = game:GetService("SoundService")
local plr = game.Players.LocalPlayer

local function playLocalSound(soundId)
    if isplaying then 
        return
    end
    isplaying = true
    local sound = soundService.Sound
    sound.SoundId = soundId
    soundService:PlayLocalSound(sound)
    sound.Ended:Wait()
    isplaying = false
end

local function onTouch(part)
    playLocalSound(6519564325)

    local h = hit.Parent:FindFirstChild("Humanoid")

    if h then 
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if player.leaderstats.Grades.Value > 0 then
                local selling = player.leaderstats.Grades
                local getting = player.leaderstats.Money

                getting.Value = getting.Value + selling.Value
                selling.Value = 0
            end
        end
    end
end

part.Touched:Connect(onTouch)
0
It didn't work, sorry for the late response TudiPYM 12 — 3y
0
The output says "Failed to load sound 6519564325: Unable to download sound data" TudiPYM 12 — 3y
Ad

Answer this question