PlayerAdded event not running

My .PlayerAdded event doesnt run when the game starts.

print("Beginning script")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	print("Player added")
	Player.CharacterAdded:Connect(function(Character)
		print("Character added")
		print("Creating Instance...")
		
		local BodyAttach = Instance.new("Motor6D")
		BodyAttach.Name = "BodyAttach"
		BodyAttach.Parent = Character.HumanoidRootPart
		
		print("Instance created!")
	end)
end)

print("Ending script")

Only the first and last print statements print.

Where exactly is this script…? It should be in ServerScriptService as a Regular Script

Have you tried printing the number of players outside of the event listener? It’s possible that the player joins before the script has a chance to run depending on how you set this up.

It is inside of a tool in Starter gear.

And I just did that. It print my name, I dont know what result you are looking for.

This is most likely the issue. Try running it from a normal script on the server.

1 Like

That worked. Why did that work? I use server scripts in tools a decent bit and they usually work.

They most likely work due to the tool being parented to your character, which is a child of workspace when it is equipped. Since this is setting up the event listener before the tool is equipped, it probably doesn’t work.

Oh right, that actually makes sense. Okay thanks!

1 Like

I’ve been told that the PlayerAdded event fails when the player is initializing a new server alone, usually we fix this problem by simply doing:

local function OnPlayerAdded(Player)
   print("Initialized")
end

for _, Player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(OnPlayerAdded)(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Even though Server Scripts may run the moment the server first starts up, it depends on where you exactly place them

In this instance, you’re putting it inside the Tool object so the PlayerAdded event would not detect your Player ahead of time until a different Player is first added onto the game, then the Event would detect its changes afterwards

If you place the script inside ServerScriptService instead, it would account for every player that joins the game

Not for the first player though, that’s what I’m talking about, the first player who joins the game is the one who initializes the server hence why we use the method I’ve mentioned above, especially for data stores.

ProfileService example making use of the method:
Basic Usage - ProfileService (madstudioroblox.github.io)

That’s only if it fails though, but fair enough

The PlayerAdded Event should work most of the time regardless though, but if for some reason it doesn’t yes you’d need to account for the first player

local Players = game:GetService("Players")

local function PlayerAddedFunction(Player)
    print(Player.Name)
end

for _, Player in pairs(Players:GetPlayers()) do
    PlayerAddedFunction(Player)
end

Players.PlayerAdded:Connect(PlayerAddedFunction)
1 Like