Help with music script

Pretty simple idea: when a brick is touched, play a song (I can do that). When a different brick is touched, stop the current song and play a new one!

What I have


Note:

  1. The brick that alternates transparency isn’t necessary, added it there for testing.
  2. I picked two random songs from the front page, feel free to change them (I will end up using different songs eventually).

Thanks in advance,
Yolo

1 Like

Could you paste your script into the post? It would be easier than just writing it from an image.

Every time a player touches the part, it will play. This is regardless if the player even rotates their character on the part. I recommend using a debounce so it will only play every second or so depending on how long the audio is playing. You should also check to make sure that you’re actually getting a player. Use an if statement to check if the part’s parent has a Humanoid.

local sound = script.Parent.Bruh
local partsound = game.Workspace.Red.san
local part = script.Parent
local red = game.Workspace.Red

part.Touched:Connect(function(bruh)
    local plr = game.Players:GetPlayerFromCharacter(bruh.Parent)
    if plr then
        sound:Play()
        partsound:Stop()
        partt.Transparency = 1
    end
end)

red.Touched:Connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    if plr then
        sound:Stop()
        partsound:Play()
        partt.Transparency = 0
    end
end)

I just noticed that the second variable local sound isn’t finding the child correctly. What is should be.

local sound = script.Parent.bruh

This is assuming the script is inside the part black

Good point. I just fixed that, but the music still isn’t working.

1 Like

I’ll try to do those things. I’m really not great with scripting, so I don’t know how to, but I’ll use the wiki.

Issue with your script I noticed. Not sure if this is what you’re looking for as you didn’t explicitly state what’s wrong.

The issue with your script is that there’s no debounce. I’m going to simply add an additional conditional to your script to check if the sound is already playing.

local sound = script.Parent.Bruh
local partsound = game.Workspace.Red.san
local part = script.Parent
local red = game.Workspace.Red

part.Touched:Connect(function(bruh)
    local plr = game.Players:GetPlayerFromCharacter(bruh.Parent)
    if plr and sound.Playing == false then
        sound:Play()
        partsound:Stop()
        partt.Transparency = 1
    end
end)

red.Touched:Connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    if plr and partsound.Playing == false then
        sound:Stop()
        partsound:Play()
        partt.Transparency = 0
    end
end)

I literally told him to do that…

1 Like

Another thing, every time you run your touched function, you are overwriting a previous variable part. On line 4 of your image, you specify the variable part as script.Parent. On line 16, you set part as the object that touched red.

I apologize if this is a stupid question, but should I then just change the variable name?

Since the variable goes across the entire code, it’s recommended you do that. If you had the variable only defined in a function, it wouldn’t matter. But in your case, I recommend you change the variable name.

Here is the script. You has some variable mistakes, but it’s fixed. Good attempt and keep on learning! Tell me if anything does not work. It is important to mention that @chexburger and @8w_a are correct and you should use a debounce since it would keep looping when you touch the parts.

local sound = script.Parent.Bruh
local partsound = workspace.Red.san
local part = script.Parent
local red = workspace.Red
local Debounce1 = false
local Debounce2 = false


part.Touched:Connect(function(Object)
    local plr = game.Players:GetPlayerFromCharacter(Object.Parent)
    if plr then
	if not Debounce1 then       
	Debounce1 = true
		red.Transparency = 0 
		part.Transparency = 1 
		sound:Play()
		partsound:Stop()
	Debounce2 = false
    end
end
end)

red.Touched:Connect(function(Hit)
    local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if plr then
if not Debounce2 then  
	Debounce2 = true
		part.Transparency = 0
		red.Transparency = 1     
		sound:Stop()
    	partsound:Play()
	Debounce1 = false
    end
end
end)

Thank you @chexburger @8w_a and @xWil_l for the solution, all of you were correct and it works fine!

3 Likes