I want to achieve a system that counts players footsteps on the leaderstats!
What is the issue?
I don’t really know how to like count the players footsteps!
What solutions have you tried so far?
Find a solution!
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Steps = Instance.new("NumberValue")
Steps.Name = "Steps"
Steps.Parent = leaderstats
Steps.Value = 0
local key = player.UserId
local data
local success,msg = pcall(function()
data = DataStore:GetAsync(key)
end)
if data then
print("Data Loaded")
Steps.Value = data.S
else
print("Error")
warn(msg)
end
player.Character.HumanoidRootPart.Running:Connect(function(speed)
if speed > 0 then
player.leaderstats.Steps.Value += 1
else
player.leaderstats.Steps.Value += 0
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local data = {
S = player.leaderstats.Steps.Value,
}
local success,msg = pcall(function()
DataStore:SetAsync(key,data)
end)
if success then
print("Data Saved!")
end
end)
I think those are the easiest ways and I just realised that you
wrote HumanoidRootPart.Running, but the HumanoidRootPart doesn’t have a running event, change it to just Humanoid.Running to detect when the player is moving.
local steps = 0
local humanoid = humanoid.here
while task.wait() do
if humanoid.MoveDirection ~= Vector3.new() then
print("Player is walking!")
steps += 1
else
print("Player isnt moving.")
end
end
Heres an example code I’ve written for you.
To fix standing still or jump giving steps you can check if humanoid is in running state or not by:
Humanoid:GetState()
The code:
Without GetState()
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local val = Instance.new("NumberValue",folder)
val.Name = "Steps"
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
spawn(function()
while task.wait() do
if humanoid.MoveDirection ~= Vector3.new() then
val.Value += 1
end
end
end)
end)
end)
With GetState()
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local val = Instance.new("NumberValue",folder)
val.Name = "Steps"
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
spawn(function()
while task.wait() do
if humanoid.MoveDirection ~= Vector3.new() then
if humanoid:GetState() == Enum.HumanoidStateType.Running then
val.Value += 1
end
end
end
end)
end)
end)
Your Fixed Code
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Steps = Instance.new("NumberValue")
Steps.Name = "Steps"
Steps.Parent = leaderstats
Steps.Value = 0
local key = player.UserId
local data
local success,msg = pcall(function()
data = DataStore:GetAsync(key)
end)
if data then
print("Data Loaded")
Steps.Value = data.S
else
print("Error")
warn(msg)
end
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid") or char:FindFirstChild("Humanoid")
spawn(function()
while task.wait() do
if humanoid.MoveDirection ~= Vector3.new() then
if humanoid:GetState() == Enum.HumanoidStateType.Running then
Steps.Value += 1
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local data = {
S = player.leaderstats.Steps.Value,
}
local success,msg = pcall(function()
DataStore:SetAsync(key,data)
end)
if success then
print("Data Saved!")
end
end)