Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make it so a player gets 1 point every second when walking?

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.


amount = 1
points = "Steps"

game.Players.PlayerAdded:connect(function(p)
	p.CharacterAdded:connect(function(c)
		c:WaitForChild("Humanoid").Running:connect(function(speed)
			if speed > 0
				then p.leaderstats[points].Value = p.leaderstats[points].Value + amount
			end
		end)
	end)
end)

I also have another script that goes with this one to set up the scoreboard here:


game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new("BoolValue",plr)
	stats.Name = "leaderstats"

	local steps = Instance.new("IntValue",stats)
	steps.Name = "Steps"
	steps.Value = 0
end)

3 comments

0

Also, I think the problem has to do with c:WaitForChild("Humanoid").Running:connect(function(speed)

0

is the game FE? where are the scripts located and what type of scripts are thye

0

Try using a loop and check every second if they are moving.

3 answers

✓ Accepted answer

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:

lcoal amount = 1;
local points = "Steps";
local wte = wait;
local vec3 = Vector3;

local function CheckForWalk(plr, hum)
	pcall(function()
		while wte() do
			if hum.MoveDirection ~= vec3.new(0, 0, 0) then --if the character is moving
				while wte(1) do --while the player is moving, increase their step points each second.
					if hum.MoveDirection ~= vec3.new(0, 0, 0) then
						plr.leaderstats[points].Value = plr.leaderstats[points].Value + amount;
					else
						break;
					end
				end
			end
		end
	end);
end

game.Players.PlayerAdded:connect(function(plr)
	repeat
		wte();
	until plr.Character;
	local hum = plr.Character:WaitForChild("Humanoid");
	CheckForWalk(plr, hum);
	plr.Changed:connect(function(prop) --we need to re-call the CheckForWalk function when the player dies, because the old one would have halted execution if the old humanoid dies.
		if prop == "Character" then --make sure that the Character is a different model than before
			repeat wte() until plr.Character;
			local hum = plr.Character:WaitForChild("Humanoid");
			CheckForWalk(plr, hum);
		end
	end);
end);

I hope this helps.

1 comment

0

I was testing, and I found that if you die, it wont give you points anymore. Do you know why?

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:

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local running = false
		char:WaitForChild("Humanoid").Running:connect(function()
			if running == false then
				running = true
				while char.Torso.Velocity.magnitude > 0 do
					wait(1)
					plr:WaitForChild("leaderstats").points.Value = plr.leaderstats.points.Value + 1
					print(char.Torso.Velocity.magnitude)
				end
				running = false
			end
		end)
	end)
end)

2 comments

0

Okay, thank you!

0

np

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

local plr = game.Players.LocalPlayer
local char = workspace:WaitForChild(plr.Name)
local Humanoid = char:WaitForChild("Humanoid")
while true do
	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		print("Walking")
		plr:WaitForChild("leaderstats").points.Value = plr.leaderstats.points.Value + 1
	else
		print("Standing")
	end
	wait(1)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.