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)
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)
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)
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.)
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.
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.
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.
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).
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.