I was looking at alvinblox on youtube and he was explaining how to make music in different sound environments. However, I seen some flaws within the code with playing music.Whenever, I jump it re fires the code again and plays the music again. I just want the music to continue to play WITHOUT RESTARTING until we leave the brick. Thanks in advance.
I've tried:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild('HumanoidRootPart') then game.Players[hit.Parent.Name].PlayerGui.Sound2:Stop() game.Players[hit.Parent.Name].PlayerGui.Sound1.Volume = 1 game.Players[hit.Parent.Name].PlayerGui.Sound1:Play() end end)
and
Debounce = true script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild('HumanoidRootPart') and Debounce then Debounce = false game.Players[hit.Parent.Name].PlayerGui.Sound2:Stop() game.Players[hit.Parent.Name].PlayerGui.Sound1.Volume = 1 game.Players[hit.Parent.Name].PlayerGui.Sound1:Play() end end)
and
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild('HumanoidRootPart') and game.Players[hit.Parent.Name].PlayerGui.Sound1.IsPlaying == false then game.Players[hit.Parent.Name].PlayerGui.Sound2:Stop() game.Players[hit.Parent.Name].PlayerGui.Sound1.Volume = 1 game.Players[hit.Parent.Name].PlayerGui.Sound1:Play() end end)
everytime I jump it still RESTARTS the music.
I want the music to continously play in the area until I leave.
A server script can't make changes in the player's PlayerGui
, only a LocalScript
can.
Instead of using the Touched event, use DistanceFromCharacter
.
Place this LocalScript in StarterCharacterScripts
under the StarterPlayer folder.
repeat wait() until game.Players.LocalPlayer repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace) ------------------------------------------------------------------------------------------------------------- --// CONSTANTS local Player = game.Players:WaitForChild(game.Players.LocalPlayer.Name) local PlayerGUI = Player:WaitForChild('PlayerGui') local Character = Player.Character --// VARIABLES local Part = game.Workspace:WaitForChild('Part') -- place a part in the center of the region where Sound1 will play local DetectDistance = 50 -- required distance to play Sound1 (if the Player is near the Part within a 50 stud radius, Sound1 will play) local Distance = nil local Sound1 = PlayerGUI:WaitForChild('Sound1') local Sound2 = PlayerGUI:WaitForChild('Sound2') --// FUNCTIONS function DETECT_PART() Distance = Player:DistanceFromCharacter(Part.Position) print(Player.Name .. " is " .. Distance .. " studs away from " .. Part.Name) if (Distance <= DetectDistance) then -- checks if Distance is equal to or less than DetectDistance (50 studs) if not Sound1.Playing then Sound2:Stop() Sound1:Play() end elseif (Distance > DetectDistance) then if Sound1.Playing then Sound1:Stop() Sound2:Play() end end end Character.Humanoid.Running:connect(DETECT_PART) -- Use the loop below if the Running event is buggy --while true do -- DETECT_PART() -- wait() --end