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

Why is my script for playing audio for a specific player not working?

Asked by 4 years ago

Why doesnt this work?:

local Players = game:GetService("Players")
local name = "yourfuturepilot2" 




Players.OnPlayerAdded:Connect(function(player)
    print("yes")
    local audio = player.PlayerScripts.audio
    if player.Name == name then
        audio:Play()
        print("audio has played for player")
    end
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.

0
Change Players.OnPlayerAdded to game.Players.PlayerAdded instead. Dovydas1118 1495 — 4y
0
Tried that before this post, it didn't work. yourfuturepilot2 -2 — 4y
0
Unfortunately, PlayersAdded wont work for what @Dovydas1118 said, I believe, as it is local. Also, do not use your username. Use your userID. This will never change, so you can change your name without changing your code. Your account userID is 125293735. I found this in the URL for your profile. Tachankas greatest. SuchASaltyLemon 35 — 4y

3 answers

Log in to vote
2
Answered by
ImTrev 344 Moderation Voter
4 years ago
Edited 4 years ago

You said resolved but if you want the audio to play for everyone you should change your code back to:

local Players = game:GetService("Players")
local name = "yourfuturepilot2" 

Players.PlayerAdded:Connect(function(player)
    print("yes")
    local audio = player.PlayerScripts.audio
    if player.Name == name then
        audio:Play()
        print("audio has played for player")
    end
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.

0
The point of the script was to make it just play for me, sorry if I wasn't specific enough. Thanks for the help though, that might be useful in the future. yourfuturepilot2 -2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

RESOLVED I changed it to

local audio = game.Players.LocalPlayer.PlayerScripts.Audio
if game.Players.LocalPlayer.Name == "yourfuturepilot2" then -- players name here
    audio:Play()
    print("audio has played for player")
end

0
f I was working on a solution lol Gooncreeper 98 — 4y
0
OOF sorry yourfuturepilot2 -2 — 4y
0
So do you want the audio just to play for you and no one else? ImTrev 344 — 4y
Log in to vote
0
Answered by 4 years ago

You cannoy use the PlayerAdded event as this is a local script. To get the local player, it should looks something like this:

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local chosenID = 125293735

local audio = player.PlayerScripts.Audio

if player.UserId == chosenID then
    audio:Play()
    print("Audio played for " .. player.Name .. " - " .. player.UserId)

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!

Answer this question