How to make a distance traveled leaderstat

How do i make a km traveled stat on Roblox. It seems very difficult. I have tried using magnitude but that wouldnt work out for me so how do i make a distance traveled leaderstat/stat

I’m pretty sure there is a property of Humanoid that says if its walking or not.
if it is walking, it would take 15 seconds at 16 walkspeed to walk a km.

2 Likes

yeah but how do i check for how long a player has walked

What’s the problem with using magnitude? You can calculate it on the client and send to the server or calculate it on the server. Server calculations with anti exploits are more reliable than client calculations, of course.

Here’s how you can calculate the traveled distance on the client. On the server, it’s similar, but you’ll need to get the player reference some other way.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer

plr.CharacterAdded:Connect(function()
    local studsTraveled = 0
    local hum, hrp = char:WaitForChild("Humanoid"), char:WaitForChild("HumanoidRootPart")
    local pos = hrp.Position
    while char.Parent and hum.Health > 0 do
        local newPos = hrp.Position
        studsTraveled += (newPos-pos).Magnitude
        pos = newPos
        RunService.Heartbeat:Wait()
    end
end)

I tried sending the data over a remote event but that didn’t work

Could you show the code you are using to send it?

stat script:

game.Players.PlayerAdded:connect(function(p)
local tts= 0
local stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”
stats.Parent = p

local money = Instance.new(“IntValue”)
money.Name = “Distance”
money.Value = 0
money.Parent = stats

end)

—send stats

game.ReplicatedStorage.UpdateTraveledDist.OnServerEvent:Connect(function(plr, studsTraveled)

plr.leaderstats.Distance.Value = studsTraveled

end)

-----client script (the one you gave)

local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)

local plr = Players.LocalPlayer

plr.CharacterAdded:Connect(function(char)
local studsTraveled = 0
local hum, hrp = char:WaitForChild(“Humanoid”), char:WaitForChild(“HumanoidRootPart”)
local pos = hrp.Position
while char.Parent and hum.Health > 0 do
wait()
local newPos = hrp.Position
studsTraveled += (newPos-pos).Magnitude
pos = newPos
game.ReplicatedStorage.UpdateTraveledDist:FireServer(studsTraveled)
print(studsTraveled)
end
end)