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)
Hey DarkAssasin0,
:Play()
. Now, this is how you actually play the Sound itself. Below is a personal example of this method/function in action.local sound_to_play = workspace.Sound; sound_to_play:Play(); -- Simply plays the sound.
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.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)
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.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)
: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.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)
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.~~ KingLoneCat