Hello, I have been trying to figure out how to make it give 1 point every second that a player is walking. When I click "play" and walk, it gives too many points. I tried putting "wait(1)" in various locations, but it ends up just delaying the amount of points being given.
01 | amount = 1 |
02 | points = "Steps" |
03 |
04 | game.Players.PlayerAdded:connect( function (p) |
05 | p.CharacterAdded:connect( function (c) |
06 | c:WaitForChild( "Humanoid" ).Running:connect( function (speed) |
07 | if speed > 0 |
08 | then p.leaderstats [ points ] .Value = p.leaderstats [ points ] .Value + amount |
09 | end |
10 | end ) |
11 | end ) |
12 | end ) |
I also have another script that goes with this one to set up the scoreboard here:
1 | game.Players.PlayerAdded:connect( function (plr) |
2 | local stats = Instance.new( "BoolValue" ,plr) |
3 | stats.Name = "leaderstats" |
4 |
5 | local steps = Instance.new( "IntValue" ,stats) |
6 | steps.Name = "Steps" |
7 | steps.Value = 0 |
8 | end ) |
You can use the
Humanoid.MoveDirection
property to determine if the Humanoid is in motion or not.
Humanoid.MoveDirection
is a Vector3
that contains data regarding the direction that the humanoid is moving in. Likewise if the Humanoid is not moving at all, it will be 0, 0, 0
, therefore you simply need to check if the Humanoid's MoveDirection is not equal to Vector3.new(0, 0, 0)
to determine whether or not you should add a point for the elapsed second.
-Here is a basic setup:
01 | lcoal amount = 1 ; |
02 | local points = "Steps" ; |
03 | local wte = wait; |
04 | local vec 3 = Vector 3 ; |
05 |
06 | local function CheckForWalk(plr, hum) |
07 | pcall ( function () |
08 | while wte() do |
09 | if hum.MoveDirection ~ = vec 3. new( 0 , 0 , 0 ) then --if the character is moving |
10 | while wte( 1 ) do --while the player is moving, increase their step points each second. |
11 | if hum.MoveDirection ~ = vec 3. new( 0 , 0 , 0 ) then |
12 | plr.leaderstats [ points ] .Value = plr.leaderstats [ points ] .Value + amount; |
13 | else |
14 | break ; |
15 | end |
I hope this helps.
I tried it out myself. I noticed the the Running event for Humanoid will fire multiple times even if you just move once, that's the cause of it giving multiple points at once. To fix that, I made a debounce for the script. I also noticed that with your method, speed is a constant and will forever add points once you get into motion once, so I used c.Torso.Velocity.magnitude
instead.
Here's the final script I got working for myself:
01 | game.Players.PlayerAdded:connect( function (plr) |
02 | plr.CharacterAdded:connect( function (char) |
03 | local running = false |
04 | char:WaitForChild( "Humanoid" ).Running:connect( function () |
05 | if running = = false then |
06 | running = true |
07 | while char.Torso.Velocity.magnitude > 0 do |
08 | wait( 1 ) |
09 | plr:WaitForChild( "leaderstats" ).points.Value = plr.leaderstats.points.Value + 1 |
10 | print (char.Torso.Velocity.magnitude) |
11 | end |
12 | running = false |
13 | end |
14 | end ) |
15 | end ) |
16 | end ) |
You can make a loop where it checks if MoveDirection (A property in Humanoids) is not Vector3.new(0,0,0) (Standing Still) and give the player points based on that
01 | local plr = game.Players.LocalPlayer |
02 | local char = workspace:WaitForChild(plr.Name) |
03 | local Humanoid = char:WaitForChild( "Humanoid" ) |
04 | while true do |
05 | if Humanoid.MoveDirection ~ = Vector 3. new( 0 , 0 , 0 ) then |
06 | print ( "Walking" ) |
07 | plr:WaitForChild( "leaderstats" ).points.Value = plr.leaderstats.points.Value + 1 |
08 | else |
09 | print ( "Standing" ) |
10 | end |
11 | wait( 1 ) |
12 | end |