Counting the amount of studs that every player has walked?

hi.
I am making a game that requires the counting of how many studs each player has walked.
How would I do this?

Is doing an infinite loop in one serverscript the best way to manage all players stud walk count?

I am wanting the best way to do this, that also cannot be easily hacked if there is a solution that uses client sided things.

What would be the best solution?

2 Likes

Humanoid.MoveDirection, a unit vector that changes based on where the user is headed. It’ll have a magnitude of 1 if the player is moving, and 0 if not

You could use .Magnitude to compare the last position the player’s HumanoidRootPart was in to a new position.

game.Players.PlayerAdded:Connect(function(plr)
	wait(5)
	local prev = plr.Character.HumanoidRootPart.Position
	while wait(5) do
		local distance = ((plr.Character.HumanoidRootPart.Position - prev).Magnitude)
		print(distance)
		prev = plr.Character.HumanoidRootPart.Position
	end
end)

Every 5 seconds, this will get the distance the player has moved, and store it in the distance variable. It is a server script, so put it in ServerScriptService. Cheaters will also not be able to mess with the value.

1 Like

Great.
Sorry not super advanced with scripting… but since it is a Server Script and there is only the distance variable, will the distance variable be different for every player?

Say if I have a TextLabel in StarterGui, could I set the text in the TextLabel to distance, and it will be different for everyone?

It should be, I would use a RemoteEvent to tell that specific player their distance though so that they cannot get mixed up. Something like this:

game.Players.PlayerAdded:Connect(function(plr)
	wait(5)
	local prev = plr.Character.HumanoidRootPart.Position
	while wait(5) do
		local distance = ((plr.Character.HumanoidRootPart.Position - prev).Magnitude)
		print(distance)
		prev = plr.Character.HumanoidRootPart.Position
		game.ReplicatedStorage.REMOTENAMEHERE:FireClient(plr, distance) --replace REMOTENAMEHERE with your remote name
	end
end)
2 Likes

Put this in a script that’s in ServerScriptService:

--//Services
local Players = game:GetService("Players")

--//Functions
Players.PlayerAdded:Connect(function(player)
	local totalDistance = 0
	local previousPosition = nil
	
	player.CharacterAdded:Connect(function(character)
		task.defer(function()
			previousPosition = character.PrimaryPart.Position
		end)
		
		while task.wait(0.5) and character:IsDescendantOf(workspace) do
			local newDistance = (character.PrimaryPart.Position - previousPosition).Magnitude
			
			if newDistance >= 0.1 then
				totalDistance += newDistance
			end
			
			print(player.Name .."'s total distance:", math.floor(totalDistance), "studs")
			
			previousPosition = character.PrimaryPart.Position
		end
	end)
end)
1 Like