Hello Developers!
I was making my game and wanted to print something if the owner joins. The script was not working and I didn’t know how to fix it. The script is in ServerScriptService and it was a regular script not a module, or Local script.
local player = game.Players.LocalPlayer
if player.Name == "TrackoTheTaco" then
print("test")
end
Instead of this make a Script in ServerScriptService and try this:
game.Players.PlayerAdded:Connect(function(player)
if player.Name == "TrackoTheTaco" then
print("test")
end
end)
Let me know if it works, if it does then mark it as the solution so others know.
What the script does is when a player joins the game and the player name is “TrackoTheTaco” it will print(“test”), if you want it to be a local script then you can do the same but instead of a script put the code in a local script in StarterPlayerScripts inside of StarterPlayer.
The issue here is that you are trying to run game.Players.LocalPlayer inside of a Server Script, which would not work, as it is not a local script. You can trying using player.UserId to achieve this:
local Player = game:GetService("Players"):GetPlayerByUserId() --Insert your userid here
if Player then
print("Test.")
end```
Your solution works but it has a flaw in it, the script can run very fast before the player even joins the game and it will just error because it couldn’t find the player.
But if you use my solution it will never error because I made it so when the player joins the game the code inside it will fire.