Count players footsteps!

  1. What do you want to achieve?

I want to achieve a system that counts players footsteps on the leaderstats!

  1. What is the issue?

I don’t really know how to like count the players footsteps!

  1. 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)

Thank you in advance!

You can check for specific keyframes or markers with these if they are your own animations:

Alternatively you can use a while loop while the walk animation is playing and estimate when to add the step.

Is there any easier way of doing this?

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.

You can check if humanoid is walking by using:

  • Humanoid.MoveDirection

Example:

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

How can I get Humanoid in a server script

The same way you would get Humanoid in a Local Script.

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)
2 Likes

@Synitx, you are doing it in a really wrong way.