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

Music that plays after entering a zone?

Hello! I made this script, where if the player touches a part that is settled at the entrance of the building a music will play and once he leaves it will stop.

script.Parent.Touched:Connect(function(plr)
	    local player = game.Players:GetPlayerFromCharacter(plr)
		local music = player.PlayerGui.Musics.MusicOne
        local musictwo = player.PlayerGui.Musics.MusicTwo
        local humanoid = plr:WaitForChild("Humanoid")
        if humanoid then 
		if music.IsPlaying == false then
			if musictwo.IsPlaying == false then
				repeat until music.IsPlaying == false and musictwo.IsPlaying == false
				music:Play()
				wait(104)
				music:Stop()
				wait(1.5)
				musictwo:Play()
				wait(85.5)
			end
	elseif music.IsPlaying == true or musictwo.IsPlaying == true then
		music:Stop()
		musictwo:Stop()
		end
	end
end)

Unfortunately, it is not working. Who can help me? Please I am desperate! xD

5 comments

0

use a debounce

0

I would recommend having it so the script only plays while touching the brick or have a invisible walkthroughable brick that you can hit that plays it while touching that brick. That or try looking into some sound groups.

0

@KDarren12 I don't really know how. Could you help me?

0

I'll post some code to help, soon, today. There's a lot of things that could be going wrong, such as the music not completely loaded into the client..

0

better to use region3 for this but use whatever idc lol, use a debounce tho

1 answer

see here and here and here

script.Parent.Touched:Connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local music = player.PlayerGui.Musics.MusicOne
        local musictwo = player.PlayerGui.Musics.MusicTwo
        local humanoid = plr:WaitForChild("Humanoid")
        if hit.Parent:FindFirstChild("Humanoid") then 
        if music.IsPlaying == false then
            if musictwo.IsPlaying == false then
                repeat wait () until music.IsPlaying == false and musictwo.IsPlaying == false
                music:Play()
                wait(104)
                music:Stop()
                wait(1.5)
                musictwo:Play()
                wait(85.5)
            end
    elseif music.IsPlaying == true or musictwo.IsPlaying == true then
        music:Stop()
        musictwo:Stop()
        end
    end
end)

New Code:

--made 100% of Eternalove_fan32--
-- Insert this ServerScript in a part with colision false
local part = script.Parent
debounce = false
part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		local random = math.random(1,2)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local MusicOne = Instance.new("Sound", player)
		MusicOne.SoundId = "rbxassetid://2218540019"
		MusicOne.Name = "MusicOne"
		local MusicTwo = Instance.new("Sound", player)
		MusicTwo.SoundId = "rbxassetid://2588710875"
		MusicTwo.Name = "MusicTwo"
		if random == 1 then
			MusicOne.Playing = true
		else
			MusicTwo.Playing = true
		end
		debounce =  true
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == true then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		for i,v in pairs(player:GetChildren())do
			if v:IsA("Sound")then
				v:Destroy()
			end
		end
		debounce =  false
	end
end)

-- New Edites V.2

--Credits to Alvin blox
local SoundsInRegion = game.Workspace:WaitForChild("SoundsRegions")

local Found = false

while wait(1)do
	
	for i,v in pairs(SoundsInRegion:GetChildren())do
		
		Found = false
		local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
		
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
		
		for _, part in pairs(parts)do
			
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				
				print("Player was Found")
				Found = true
				break
				
			else
				
				Found = false
				print("Player was not Found in region")
				
			end
			
		end
		
		if Found == true then
			
			if script.Parent.Sounds[v.Name].IsPlaying == false then
				
				script.Parent.Sounds[v.Name]:Play()
				break
			end
		else
			
			script.Parent.Sounds[v.Name]:Stop()
			
		end
		
	end
	
end

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