Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

3 comments

0

Change Players.OnPlayerAdded to game.Players.PlayerAdded instead.

0

Tried that before this post, it didn't work.

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.

3 answers

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.

1 comment

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.

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
	

3 comments

0

f I was working on a solution lol

0

OOF sorry

0

So do you want the audio just to play for you and no one else?

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.