PlayerAdded not working

When a player, joins, his/her name still isnt printed. Am i doing something wrong with my script.

local Train = script.Parent -- Change this to the train
local Rep = game.ReplicatedStorage
local Players = game:GetService("Players")
wait(2)



Players.PlayerAdded:Connect(function(player)
	print("PLEASE PRINT GOSH")
	print(player.Name)
	for i,v in pairs(Train:GetDescendants()) do
		if v:IsA("BasePart") and not v.Anchored then
			v:SetNetworkOwner(player)
		end
	end
end)



The wait(2) at the start probably hooks the event up after you join.

Well for one, remove the wait. Second, try making it a handling function and handle any players that joined before the script loaded.

local Train = script.Parent -- Change this to the train
local Rep = game.ReplicatedStorage
local Players = game:GetService("Players")

function handleplayer(player: Player)
	print("PLEASE PRINT GOSH")
	print(player.Name)
	for i,v in pairs(Train:GetDescendants()) do
		if v:IsA("BasePart") and not v.Anchored then
			v:SetNetworkOwner(player)
		end
	end
end

for i,v in pairs(game.Players:GetPlayers()) do handleplayer(v) end
Players.PlayerAdded:Connect(handleplayer)
3 Likes

here this script:

local Train = script.Parent -- Change this to the train
local Rep = game.ReplicatedStorage

game.Players.PlayerAdded:Connect(function(player)
	task.wait(0.1)
	print("PLEASE PRINT GOSH")
	print(player.Name)
	for i,v in pairs(Train:GetDescendants()) do
		if v:IsA("BasePart") and not v.Anchored then
			v:SetNetworkOwner(player)
		end
	end
end)

this script will work!

btw should this be a local or server script

It should be a server script since it is setting the network owner.

1 Like

Because it is setting network ownership, it is clearly a server script.
I suggest you use this solution.

Note:
Next time I suggest you don’t go on the devforum asking about what seems like a random freemodel you found.

1 Like

Easiest way to do it is just to delete the wait. The script can be server sided or local. It does not matter. What matters is where you put the script.

(It will run fine on client but if you want your network ownership to work, it has to be on the server. Thanks to @seancool07777 for pointing this out.)

1 Like

my is also working, i tried it…

1 Like

Alright

//////////////////////////////

Not necessarily in all games. Your script does not account for a large game where it may take longer for the script to begin execution. But yes, your script should work but may not for all games.

Every piece of code posted here “works” but it may not be what OP wants. There are also some different “variants” of code that all do the same thing but are different.

It all comes down to where the script is located.

Mine is more optimized and best for use, mine accounts for players that have joined before script execution, which larger and laggier games usually have issues with. Sure, yours works perfectly fine, but there will be times where it won’t and it will break the game.

1 Like

Indeed. If the priority of execution is low, the player may join before the event is connected. That is why I suggested using that solution over others as they said they were already having some problems without the wait.

1 Like

okay, then nevermind, i am not so good at scripting XD

1 Like

You should move the loop above the PlayerAdded Event. That way, it prints out all the players, then detects when a new player joins (it will double print the latest player that joined otherwise).

1 Like

No, it’s good, I learned that the hard way when players started having issues with data loading and anything else in the game. It’s more of experience being needed for that one.

2 Likes

I find it very hard to believe that a player will join within the few milliseconds it takes for the connection, but alright.

That could really happen either way, but the server execution time has to be really bad for that to happen, so it really isn’t much to worry about.