Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make this sound play when the player enters a zone?

Asked by 7 years ago

Im trying to make it so that when the player enters into the CanCollide, Transparenct, Anchored part, a song begins to play. Heres the script I have but it wont work! please help!

local sound = script.Parent

sound.Touched:connect(function()
    game.Workspace.Musics.AstraeaTheme.Playing = true
    game.Workspace.Musics.AstraeaTheme.Looped = true
end)

1 answer

Log in to vote
5
Answered by 7 years ago
Edited 7 years ago

Hey DarkAssasin0,

There's about 1 issue with your script that I found you need to make your script work if what you've said in the script is true, such as AstraeaTheme being a Sound object with a pre-loaded id. Anyway, I've found one issue that will fix your script however, you will need more then to just fix this issue because of your purpose for this script. You want the function to activate just once unless they re-enter the part. First, I will go over the issue and then some enhancements you can make to your code.


Issue

The one issue I found in your game is when you tried to play the Sound object by setting the boolean value of Playing to true. That's not the correct way to play the Sound object. There's an easy fix to this. All you have to do is explore some methods/functions of the Sound object and you will find that it has a function/method called :Play(). Now, this is how you actually play the Sound itself. Below is a personal example of this method/function in action.

Issue Example A

local sound_to_play = workspace.Sound;

sound_to_play:Play(); -- Simply plays the sound.

Enhancements

Need to Check for Character

So, basically your Touched function will run now at any time because the Touched event is activated everytime a Part touches it, and you're not checking if the Part that touches it is belonging to a Character or not. For this, you will need to check if there's an object inside of the Player with the ClassName of 'Humanoid'. That's an object that all players have inside their Character. Below is an enhancement example of how you would go about doing this.

Enhancement Example A

local sound = script.Parent

sound.Touched:connect(function(obj)
    local hum = obj.Parent:FindFirstChildOfClass("Humanoid");
    if hum then
        game.Workspace.Musics.AstraeaTheme:Play()
        game.Workspace.Musics.AstraeaTheme.Looped = true
    end
end)

Repetition with Sound Playing

Well, from your question you want this to sound to play while the Player is inside of the part, which is why you will need to check if the Sound is playing and if it is then you need to make sure this code is not repeated. Because, ROBLOX's Touched event is activated every time a part touches it and, believe it or not, the Player is touching the part while the Player is inside of the part. Below is an enhancement example of how you would go about doing this.

Enhancement Example B

local sound = script.Parent

sound.Touched:connect(function(obj)
    local hum = obj.Parent:FindFirstChildOfClass("Humanoid");
    if hum and not game.Workspace.Musics.AstraeaTheme.IsPlaying then
        game.Workspace.Musics.AstraeaTheme:Play()
        game.Workspace.Musics.AstraeaTheme.Looped = true
    end
end)

Use Variables and :WaitForChild() method

So, this is quiet a small enhancement to your script. I recommend you use variables through out your code just in-case you need the variables later and it just makes your life so much easier. Also, I recommend you use the :WaitForChild() method of workspace, this I believe reduces lag and it's also helping your game become more StreamingEnabled-proof just in-case you need to use StreamingEnabled in the future. Below is an enhancement example of both's usage.

Enhancement Example C

local sound = script.Parent

sound.Touched:connect(function(obj)
    local hum = obj.Parent:FindFirstChildOfClass("Humanoid");
    local music = game.Workspace:WaitForChild("Musics"):WaitForChild("AstraeaTheme");
    if hum and not music.IsPlaying then
        music:Play()
        music.Looped = true
    end
end)

Tip

Well, it looks like you are using a sort of region music player and you seem to be doing it with parts. Now, it's fine to do this with parts but, I'd advise making the part that plays the music cover the entrance of the region and make the part that ends the music a different part for when the music stops playing for the Character and use the same enhancement examples I gave you above and also, make that part cover only the exit of the region as well. The only reason I'm saying you should make it a different part and not just use the TouchEnded event is because I don't think it's worth it putting your trust in the TouchEnded event because, it can fire at any time even while ur inside the part.

Alright, I hope I helped in one way or another and have a nice day/night.

~~ KingLoneCat

Ad

Answer this question