PlayerAdded too late to register new player

It seems like my PlayerAdded event doesn’t register a player being added.
This is weird on itself, but what makes it weirder is that it only happens when I insert some folders with sounds into the workspace.

This is what happens when I leave the folders out:


This is what happens when I insert them:

These are the 2 folders with sounds:


They are just sounds.

This is the script that registers player added:

local ServerScriptSrv = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local Storage = game:GetService("ServerStorage")

local BackMapsClass = require( ServerScriptSrv.MapGeneration.BackMaps )
local BackPieceClass = require( ServerScriptSrv.MapGeneration.BackPiece )
local Settings = require( script.Settings )

local BackMaps = BackMapsClass.new(Settings)

local AppendPieceEvent = script.AppendPiece
local Configuration = ServerScriptSrv.Configuration
local Pieces = Storage.Pieces

local Slocked = false
local ActiveMapGeneration = false

local Chr

Players.PlayerAdded:Connect(function(Plr)
	print("PA") -- Prints when folder is left out, doesn't print when it's in!
	if Slocked == false then
		Slocked = true
		
		Plr.CharacterAdded:Connect(function(NewChr)
			Chr = NewChr
			Chr:WaitForChild("HumanoidRootPart").Anchored = true
		end)
		
		local FirstPiece = BackMaps:NewPiece( nil, tostring(Configuration.CurrentLevel.Value), Plr )
		
		local FirstBackPieceClass = BackPieceClass.new(FirstPiece, Plr, Settings)
		FirstBackPieceClass:ThreadPiece()
		
		ActiveMapGeneration = true
		
		repeat task.wait(.1) until Chr
		
		Chr.Parent = workspace.Characters
		
		Chr:SetPrimaryPartCFrame(FirstPiece.PrimaryChunkRoot.CFrame)
		
		Chr:WaitForChild("HumanoidRootPart").Anchored = false
	elseif Slocked == true then
		Plr:Kick("Server is full.")
	end
end)

I left some unimportant stuff out of this script, but could it be that all of the stuff above the PlayerAdded or just the size of the workspace takes so long to load that it doesn’t register a player joining?

Replace

Players.PlayerAdded:Connect(function(Plr)

with

function onPlayerAdded

Replace

end)

with

end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

(apologies for the incomplete message, I hate Discourse so much it’s unreal)

1 Like

I already tried doing it that way but unfortunately, that didn’t work.

I fixed it using a method that uh… works…

I made one script that detects PlayerAdded using game.Players.PlayerAdded, inside that script are a few objects including:

  • ‘Dependecies’ IntValue
  • ‘Listening’ IntValue
  • ‘PlayerAdded’ BindableEvent

image




All other scripts that were using game.Players.PlayerAdded, now add one to the Dependencies IntValue at the very top of the script, and then I add one to the Listening IntValue at the very bottom.
Instead of listening to game.Players.PlayerAdded, the script now listens to PlayerDetector.PlayerAdded.Event

Then, inside the only script that does the game.Players.PlayerAdded I have the following code:

local Players = game:GetService("Players")

local PlrAddedBindable = script.PlayerAdded

Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Chr)
		repeat task.wait(.1) until script.Listening.Value >= script.Dependencies.Value
		
		PlrAddedBindable:Fire(Plr, Chr)
	end)
end)
3 Likes