Scripts play audio after 30 sec to a min when not meant to

I want to make it so when the player touches one part sound 1 stops and sound 2 plays and when they touch another part sound 1 plays and sound 2 stops.
The script does do what it was meant to do except for the fact that it stops sound 1 and starts sound 2 after probably 30 seconds to a minute.
I have absolutely no idea why this is happening. The player isn’t touching one of those parts to cause it to happen.
There are 4 scripts. 2 stop sound 2 play sound.

local Teleport = game.Workspace.Portal.Portal
local sound = workspace.CurrentSound

local function Touch()
	sound:Stop()
end

Teleport.Touched:Connect(Touch)


local Teleport = game.Workspace.Portal.Portal
local sound = workspace.FinalSound

local function Touch()
	sound:Play()
end

Teleport.Touched:Connect(Touch)

local Teleport = game.Workspace.Dropper13.DropperEnd1
local sound = workspace.CurrentSound

local function Touch()
	sound:Play()
end

Teleport.Touched:Connect(Touch)


local Teleport = game.Workspace.Dropper13.DropperEnd1
local sound = workspace.FinalSound

local function Touch()
	sound:Stop()
end

Teleport.Touched:Connect(Touch)


I’m not sure what might be causing this. From what I can tell no other scripts I have might be causing this. The only one that has a slight chance of possibly causing it is the script I have that stops the player from flinging.
The sound called CurrentSound has Looped set to true and Playing set to true.
The sound called FinalSound has Looped set to true and Playing set to false.

What is this for, a stage sound?

This is for when the player teleports by going into a portal. They then get teleported to a dropper (like in Minecraft except in Roblox). So yes, I guess this would be a stage sound if you mean a stage or level of a this game.

It’s a bad practice to have multiple scripts that do the same thing. Instead you should put all these stages into a folder and loop through them. Once you have done that then you need to set all the sound’s Looped property to false and Playing property to false. We do not want these sounds to loop or play when the game starts.

When the player steps on one of stage parts then you could do something like this:

...
Sound1:Play()
Sound1.Ended:Wait()
Sound2:Play()
...

This code will play one sound and once that finishes it plays the final sound.

This code will not work the intended way if you do not add a debounce, so if you do not know about debounce, I suggest you check out this thread: Best way to do a debounce - #7 by MuffinHandler

Hello, I am here to help you with your problem, I will explain everything possible, here is the solution.

-----------------/ Locations /-----------------
local PortalTeleport = game.Workspace.Portal.Portal
local DropperTeleport = game.Workspace.Dropper13.DropperEnd1
local CurrentSound = workspace.CurrentSound
local FinalSound = workspace.FinalSound

-----------------/ Functions | Method /-----------------
local Debouce = false
local function PortalTouch()
	if Debouce == false then
		Debouce = true
		CurrentSound:Stop()
		FinalSound:Play()
		task.wait(math.floor(FinalSound.TimeLength) + 0.5)
		Debouce = false
	end
end
PortalTeleport.Touched:Connect(PortalTouch)

local Debouce = false
local function DropperTouch()
	if Debouce == false then
		Debouce = true
		CurrentSound:Play()
		FinalSound:Stop()
		task.wait(math.floor(CurrentSound.TimeLength) + 0.5)
		Debouce = false
	end
end
DropperTeleport.Touched:Connect(DropperTouch)

I left the script in a folder:
image

Very well, now I will explain.

The debounce will be to stop the audio from playing repeatedly when the player touches the part.

“task.wait(math.floor(FinalSound.TimeLength) + 0.5)”

Waiting for tesk is waiting for a certain amount of time.
The math floor is to turn the audio time into an integer.

TimeLength is an audio “variable” that tells you how long the audio is, so I used this to find out if the audio had finished.

I hope I have helped.
Oh, and sorry if there was any error in the writing, I am Brazilian.

2 Likes

Sound 1 is the music for the lobby of the game. Sound 2 is for one specific “stage”. I don’t want sound 2 to start playing after sound one. I want it so when the player touches a part that teleports them to a stage/level sound 2 plays and loops until the player touches another part.

very good, I like how you explained it

Will this work specific to a single player? I don’t want a player to be playing normally and then suddenly the music changes because another player triggered the music change.

You can have a little snippet from my own code. This should work. If it does not, let me know.

local part = script.Parent
local music = nil --// Change this to your sound

local function Touched(hit)
	local character = hit.Parent
	
	if character:FindFirstChild("Humanoid") then --// Leave this in if you only want it to start when anything with a humanoid touched the part (i.e. a player, NPC, etc.) If you do not want that, then you can remove this if statement
		if not music.IsPlaying then
			music:Play()
		end
	else
		return
	end
end

local function TouchEnded(hit)
	local character = hit.Parent
	
	if character:FindFirstChild("Humanoid") then --// Leave this in if you only want it to stop when anything with a humanoid stops touching the part (i.e. a player, NPC, etc.) If you do not want that, then you can remove this if statement
		if music.IsPlaying then
			music:Stop()
		end
	else
		return
	end
end

part.Touched:Connect(Touched)
part.TouchEnded:Connect(TouchEnded)
1 Like

Would the music change only for the player that triggered it to change?

Depending on if it is in a sever script with the RunContext set to Local, or simply just in a LocalScript, then yes; however, if it is in a server script with the RunContext set to Legacy or Server, then it will change for every player.

When you say Local do you mean Client? I have no local option.

Yes. My apologies.

Character limit

image
Very simple, I used a remote event.

Here is the solution.

Local Side:

-----------------/ Locations /-----------------
local CurrentSound = workspace.CurrentSound
local FinalSound = workspace.FinalSound
local Event = script.Parent.Event
local CurrentSoundIsPlaying = false
local FinalSoundIsPlaying = false

-----------------/ Functions | Method /-----------------
Event.OnClientEvent:Connect(function(PlayCurrentSound)
	if PlayCurrentSound == true and CurrentSoundIsPlaying == false then
		CurrentSoundIsPlaying = true
		CurrentSound:Stop()
		FinalSound:Play()
		task.wait(math.floor(FinalSound.TimeLength) + 0.5)
		CurrentSoundIsPlaying = false
	elseif PlayCurrentSound == false and FinalSoundIsPlaying == false then

		FinalSoundIsPlaying = true
			CurrentSound:Play()
			FinalSound:Stop()
		task.wait(math.floor(CurrentSound.TimeLength) + 0.5)
			FinalSoundIsPlaying = false
		else
	end
end)

Server Side:

-----------------/ Locations /-----------------
local PortalTeleport = game.Workspace.Portal.Portal
local DropperTeleport = game.Workspace.Dropper13.DropperEnd1
local Event = script.Parent.Event

-----------------/ Functions | Method /-----------------
local Debouce = false
local function PortalTouch(Name)
	Event:FireClient(game.Players[Name], true)
end
PortalTeleport.Touched:Connect(function(Obj)
	if Obj.Parent:FindFirstChild('Humanoid') then
		PortalTouch(Obj.Parent.Name)
	end
end)

local Debouce = false
local function DropperTouch(Name)
	Event:FireClient(game.Players[Name], false)
end

PortalTeleport.Touched:Connect(function(Obj)
	if Obj.Parent:FindFirstChild('Humanoid') then
		DropperTouch(Obj.Parent.Name)
	end
end)

Well, if you have any questions, I will explain again.

I needed to change the script a bit to make it so when the player touched another part the music stopped. The script didn’t do anything when I tested it.

Okay. My apologies. I think I know why, but in the meantime, try what @mikegamer22445 gave you.

I noticed that the local side script doesn’t use the touch event. Unless I am missing something the local side script will not do what I’m trying to do because I need CurrentSound to stop and FinalSound to start when the player touches the portal and CurrentSound to start and FinalSound to stop when the player touches the end of the dropper. Also when I tried your first script you mentioned it set the sound that plays when the player joins to FinalSound.

I just figured out why it didn’t work. When you put local music = nil --// Change this to your sound I put the music ID. I tested it and it works now. Thanks! Also if you want to make a complete solution to this (because I had to change the script a bit) here is one of the scripts I used.

local part = script.Parent
local otherPart = game.Workspace.Dropper13.DropperEnd1
local music = game.Workspace.FinalSound --// Change this to your sound
local db = false
local db2 = false

local function Touched(hit)
	local character = hit.Parent

	if character:FindFirstChild("Humanoid") then --// Leave this in if you only want it to start when anything with a humanoid touched the part (i.e. a player, NPC, etc.) If you do not want that, then you can remove this if statement
		if not music.IsPlaying then
			if db == false then
				db = true
				music:Play()
				task.wait()
				db = false
			end
		end
	else
		return
	end
end

local function Touched2(hit)
	local character = hit.Parent

	if character:FindFirstChild("Humanoid") then --// Leave this in if you only want it to stop when anything with a humanoid stops touching the part (i.e. a player, NPC, etc.) If you do not want that, then you can remove this if statement
		if music.IsPlaying then
			if db2 == false then
				db2 = true
				music:Stop()
				task.wait()
				db2 = false
			end
		end
	else
		return
	end
end

part.Touched:Connect(Touched)
otherPart.Touched:Connect(Touched2)

I didn’t know if you wanted to make a reply to become the solution to this topic or if your fine with me doing it.

1 Like