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

Sound Glitches Once Part Is Touched?

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

✓ Accepted answer

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

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)

2 comments

0

Checking if the area value is "Lobby" it would play

0

also the sound will not change, the area value as well.

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