Why doesnt this work?:
01 | local Players = game:GetService( "Players" ) |
02 | local name = "yourfuturepilot2" |
03 |
04 |
05 |
06 |
07 | Players.OnPlayerAdded:Connect( function (player) |
08 | print ( "yes" ) |
09 | local audio = player.PlayerScripts.audio |
10 | if player.Name = = name then |
11 | audio:Play() |
12 | print ( "audio has played for player" ) |
13 | end |
14 | end ) |
It is in a local script in starterplayerscripts, and the audio is in the same place. Nothing is showing up in output, unfortunately. this is supposed to play music for someone with a specific name that enters the game. I know I am probably just being an idiot, but I am a beginner so just go easy on me.
You said resolved but if you want the audio to play for everyone you should change your code back to:
01 | local Players = game:GetService( "Players" ) |
02 | local name = "yourfuturepilot2" |
03 |
04 | Players.PlayerAdded:Connect( function (player) |
05 | print ( "yes" ) |
06 | local audio = player.PlayerScripts.audio |
07 | if player.Name = = name then |
08 | audio:Play() |
09 | print ( "audio has played for player" ) |
10 | end |
11 | end ) |
The problem was you had OnPlayerAdded
when it's really just PlayerAdded
. This script will make the audio play for everyone instead of just yourself.
RESOLVED I changed it to
1 | local audio = game.Players.LocalPlayer.PlayerScripts.Audio |
2 | if game.Players.LocalPlayer.Name = = "yourfuturepilot2" then -- players name here |
3 | audio:Play() |
4 | print ( "audio has played for player" ) |
5 | end |
You cannoy use the PlayerAdded
event as this is a local script. To get the local player, it should looks something like this:
01 | local Players = game:GetService( "Players" ) |
02 |
03 | local player = Players.LocalPlayer |
04 | local chosenID = 125293735 |
05 |
06 | local audio = player.PlayerScripts.Audio |
07 |
08 | if player.UserId = = chosenID then |
09 | audio:Play() |
10 | print ( "Audio played for " .. player.Name .. " - " .. player.UserId) |
11 |
12 | end |
Also, do not store your audio in any location that is for scripts. You can store it in SoundService or in a model. NEVER use a player name. Use player.UserId instead. A player can always change their name, but not their ID!