Player's magnitude isn't tracking correctly

My magnitude/velocity tracks when the player dies and respawns, which is what I want but it doesn’t track when the player first joins the game for some reason.

script:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local SpeedGui = PlayerGui:WaitForChild("SpeedGui")
local Label = SpeedGui:WaitForChild("Label")
local Character = LocalPlayer.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")

local function TrackSpeed()
	if RootPart and Character then
		local Number = tonumber((RootPart.Velocity * Vector3.new(1, 1, 1)).Magnitude)
		local Speed = math.round(Number)
		
		Label.Text = "S/S "..tostring(Speed)
	end
end

LocalPlayer.CharacterAdded:Connect(function()
	Character = LocalPlayer.Character
	RootPart = Character:WaitForChild("HumanoidRootPart")
	TrackSpeed()

	while wait(0.1) and Character and RootPart do
		TrackSpeed()
	end
end)

any help is appreciated!

For performance and for it to go faster, I’d recommend something like RunService.Heartbeat or RunService.RenderStepped unless you don’t want it to run too much

And with this, I can only theorize, I assume that since it’s running locally, the scripts aren’t being put into the player instantly so that’s why or something else (could you provide a video potentially?)

It’d be better if you put it in StarterCharacterScripts instead so you don’t have to wait for the CharacterAdded event. Maybe you can put it in ReplicatedFirst to make it run as soon as the player joins and before any other script.

Hello ! <3
It’s Something Simple that has already made EVERY Roblox scripter’s night.

Roblox is a slow game that takes a while to load some information, and Here is no different.
What happens is basically that the Script was loaded AFTER the Character had already Loaded.

Solving this is simple! Just create a check asking if the Player Character already exists, IF it exists, run the Function you want.

Example:

local plr = game.Players.LocalPlayer


local function YourFunction()
	
end

local function OnCharacterAdded(char)
	
	YourFunction()
	
end

if plr.Character then --Checks if a Character already exists.
	OnCharacterAdded(plr.Character)
end
plr.CharacterAdded:Connect(OnCharacterAdded) -- Connects if a New Character was created.