It currently displays 0 as the value but I’d like to make it so it changes the value to the number of studs you’ve traveled.
game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = p
local distance = Instance.new("IntValue")
distance.Name = "Distance"
distance.Value = 0 -- This is what I want to show how far you've traveled
distance.Parent = stats
end)
You would need to frequently checked the player’s current position with their previous position and calculate the length between the 2 positions and add it to the total if the length is greater than 1 stud.
For example:
local function CalculateDistance(character, distance)
local HRP = character:WaitForChild("HumanoidRootPart")
local Humanoid = character:WaitForChild("Humanoid")
local HasDied = false
-- If player dies, we use this to break the infinite loop.
Humanoid.Died:Connect(function() HasDied = true end)
local PreviousPosition = HRP.Position
repeat
task.wait(1)
local distanceTravelled = (PreviousPosition - HRP.Position).magnitude
if distanceTravelled > 1 then
distance.Value += distanceTravelled
PreviousPosition = HRP.Position
end
until HasDied
end
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local distance = Instance.new("IntValue")
distance.Name = "Distance"
distance.Value = 0
distance.Parent = stats
local character = player.Character or player.CharacterAdded:Wait()
-- If the player respawns, will need to run the function again for new character:
player.Changed:Connect(function(property)
if property == "Character" then
CalculateDistance(player.Character or player.CharacterAdded:Wait(), distance)
end
end)
-- Keep this last in the PlayerAdded:
CalculateDistance(character, distance)
end)
I’m sure there is a better way to go about this but this is just a general idea. Hope this helps!