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

Sound Glitches Once Part Is Touched?

Asked by
yoshi8080 445 Moderation Voter
8 years ago
function onTouched(hit)
local touched = false

    if not touched then 
        touched = true

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

        if check ~= nil then 

local user = game.Players:GetPlayerFromCharacter(hit.Parent)
local stats = user:findFirstChild("Area")
local sound = user.PlayerGui:FindFirstChild("Lobby")
local soundtwo = user.PlayerGui:FindFirstChild("Hi") or user.PlayerGui:WaitForChild("Hi")
local soundthree = user.PlayerGui:FindFirstChild("JC") or user.PlayerGui:WaitForChild("JC")

        if stats ~= nil then
            stats.Value = "Lobby" 
            sound:Play()
            soundtwo:Stop()
            soundthree:Stop()
            wait(1) 
            if stats.Value == 'Lobby' then
            soundtwo:Stop() 
            soundthree:Stop()       
                    end
                end
            end
        touched = false
    end
end
script.Parent.Touched:connect(onTouched)

I've made an area music script, which would change area music depending on the value. The script above is what changes the value and the music, however Once the player is still touches the part, the same music will replay. Any way to fix this?

2 answers

Log in to vote
1
Answered by
rexbit 707 Moderation Voter
8 years ago

Your problem could be due to error within the hierarchy. Try examining your Syntax, second use :WaitForChild, it's a lot more accurate than :FindFirstChild

Ad
Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
8 years ago

Check if the sound is already playing. If it is, then don't player the sound again. The debounce you added (touched = true/false) doesn't work since playing sound doesn't yield the current thread.

function onTouched(hit)
    local check = hit.Parent:FindFirstChild("Humanoid") 
    if check ~= nil then 
        local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        local stats = user:findFirstChild("Area")
        local sound = user.PlayerGui:FindFirstChild("Lobby")
        local soundtwo = user.PlayerGui:FindFirstChild("Hi") or user.PlayerGui:WaitForChild("Hi")
        local soundthree = user.PlayerGui:FindFirstChild("JC") or user.PlayerGui:WaitForChild("JC")

        if stats ~= nil and not sound.IsPlaying then -- IsPaused also works.
            stats.Value = "Lobby" 
            wait(1) 
            if stats.Value == 'Lobby' then -- any reason why this is here?
                sound:Play() -- moved it down here
                soundtwo:Stop() 
                soundthree:Stop()
            end
        end
    end
end
script.Parent.Touched:connect(onTouched)
0
Checking if the area value is "Lobby" it would play yoshi8080 445 — 8y
0
also the sound will not change, the area value as well. yoshi8080 445 — 8y

Answer this question