Can you not use game.Players.PlayerAdded on a local script?

I’m working on a script and I was running into issues on game.Players.PlayerAdded:Connect(function(plr)
my script is a local script, and i’m doing a main menu so obviously PlayerAdded is needed when making a main menu, but it is not reading it. I tested my script when i just put this on my script and nothing printed:

local SG = game:GetService("StarterGui")
local SP = game:GetService("StarterPlayer")
local PLRS = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local FT = 0
local a = Enum.CoreGuiType.All
local function SetPlayer()
SG:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
SG:SetCore("ResetButtonCallback",false)
UIS.ModalEnabled = true
print("Finished Setting GUI")
end
-- ^ this is just stuff for my main menu, below is were i put my test, 
-- as you can see i want it to print when the player is added, when i play the game
--nothing prints
game.Players.PlayerAdded:Connect(function(plr)

print("hi")

end)

In a local script you should do:

local Player = game.Players.LocalPlayer

oh and by the way this is a local script under StarterPlayerScripts

I can’t see why you would need to use this in a local script? Can you elaborate on what you need it for so we can find a workaround?

Yeah so do i just do if player then instead of game.Players.PlayerAdded?

1 Like

Yes, you should do that to make sure the player is there. If you want, but the script will be in the player so there is no point.

1 Like

don’t local scripts run when a player joins

You don’t need to use PlayerAdded because local scripts are already executed when the player joins a game.

2 Likes

its for a main menu, i want to make the player not move when the main menu is shown.
so i put the event game.players.playeradded fire the server when the player is added, and through the server it’ll change the speed and movement, etc

Oh ok, thanks for the information!

Roblox added compatibility to client side scripts so that shouldn’t be the issue.

If you read this: Players | Roblox Creator Documentation

In the notes is suggests possible problems that you might encounter when in solo mode for example. Having said that .PlayerAdded is almost certainly not the best way to achieve what you want here.

1 Like

one last question, i can change the propertys of a spawn location through a local script right? or do i have to use a server script.

You will need to use server scripts.

Technically you can, it’s just that your Player alone won’t count as it being added to the Event since LocalScripts will run the moment a player is added in

If you wanted to do it server-sided, just reference a PlayerAdded event as its own in a regular Script

Doing it client-sided, you’d need to account for the LocalPlayer as well:

local YourPlayer = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(OtherPlayer)
    print("Sanity check: ", OtherPlayer ~= YourPlayer) --This should print out true
end)

print("hi")
1 Like