Game initialization issues, WaitForChild(120), PlayerGui is not loaded and more

Hi! After I read this (awesome post btw), I still have some questions

Some players in my game are experience initialization issues… I know there should be a better and safer way to do this, what u think? There is more code obviously, this is a summary but the entire script (500 lines) is compossed like this

PlayerAdded and first lines (DataStore2)

game.Players.PlayerAdded:Connect(function(player)
local coinsStore = DataStore2("Coins",player)
local equippedStore = DataStore2("Equipped",player)

Creating variables

local Debounce = Instance.new("BoolValue") -- DEBOUNCE
Debounce.Name = "Debounce"
Debounce.Parent = dataFolder

Wait for the player (here is where I think the problem begins)

local playergui = player:WaitForChild("PlayerGui",120)
local playerdata = player.PlayerGui:WaitForChild("PlayerData",120)
local playerWork = workspace:WaitForChild(player.Name,120)
local humanoid = player.Character:WaitForChild("Humanoid",50)

Run all the functions

--------------------------------------------------------------------
-------------------// FUNCTIONS

CopyDataToLocal(player)
UpdateHUD(player)
EquipGun(player)
EquipBackpack(player)
RebirthThings(player)

So my question is, if I wait for the player in workspace, wait for PlayerGui, wait for the PlayerData folder in PlayerGui (used to store server info like gun prices, stage prices, names, etc) and ONLY THEN it starts to do things. So how is it possible that someone does not have it loaded yet?

This is not happening to much players, but it happens very often and I can’t figure out what to do

My thoughts:
*The player runs out of those 120 seconds due to lag, and the game does not load correctly

1 Like
local playergui = player:WaitForChild("PlayerGui", 120)
local playerdata = player.PlayerGui:WaitForChild("PlayerData", 120)
local playerWork = workspace:WaitForChild(player.Name, 120)
local humanoid = player.Character:WaitForChild("Humanoid", 50) -- <=

player.Character takes some time to initialize - you must wait for the character

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 50)

Also don’t forget that characters can die by void death - in this case, character must be redefined with new character (and logics related with character should stop until new character gets added)

2 Likes