roblox lua wait for player to load

-- Step 1: Define the function that waits for a player to load
function waitForPlayerToLoad(player)
    -- Step 2: Check if the player's character has loaded
    repeat
        wait() -- Step 3: Wait for a short period of time before checking again

        -- Step 4: Get the player's character
        local character = player.Character

        -- Step 5: Check if the character exists and is a descendant of the player
        if character and character:IsA("Model") and character:IsDescendantOf(game) then
            -- Step 6: Player's character has loaded, break out of the loop
            break
        end
    until not player.Parent -- Step 7: Continue looping until the player is no longer in the game
end

-- Example of how to use the function
local playerToWaitFor = game.Players.LocalPlayer -- Replace with the specific player you want to wait for
waitForPlayerToLoad(playerToWaitFor)