How to Detect if a Player is Moving for a Amount of Time

I am trying to make a script where the player walks for 2 seconds and then they run until they stop. How do I do that?

This script detects when a player moves or not if it helps

local Player = game.Players.LocalPlayer --local player
repeat wait(1) until Player.Character --repeats wait(1) until the character has loaded
local Character = Player.Character --character

local Humanoid = Character:FindFirstChild("Humanoid") --finds the humanoid from the players character

local RunService = game:GetService("RunService") --run service

local function IsPlayerMoving() --function
	if Humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
		print("Player is not moving") 
	elseif Humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
		print("Player is moving")
	end
end

RunService.RenderStepped:Connect(IsPlayerMoving) --running the function
2 Likes
local Character = script.Parent
local Humanoid:Humanoid? = Character:WaitForChild("Humanoid")
local Run = game:GetService("RunService")
function Runnn()
	print("walking")
end
Humanoid.Changed:Connect(function(Property)
	if Property == "MoveDirection" then
		if Humanoid.MoveDirection.Magnitude >= 1 then
			Run:BindToRenderStep("Runn", Enum.RenderPriority.Character.Value, Runnn)
		else
			Run:UnbindFromRenderStep("Runn")
		end
	end
end)
1 Like

That makes the player run. I want a wait(2) then the player runs

1 Like

Does it have to be exactly 2 seconds

Not exactly 2 seconds but around that

wdym? like this?

local Character = script.Parent
local Run = game:GetService("RunService")
local Humanoid:Humanoid? = Character:WaitForChild("Humanoid")
function Runnn()
	print("walking")
end
Humanoid.Changed:Connect(function(Property)
	if Property == "MoveDirection" then
		if Humanoid.MoveDirection.Magnitude >= 1 then
			task.delay(2,function()
				if Humanoid.MoveDirection.Magnitude >= 1 then
					Run:BindToRenderStep("Runn", Enum.RenderPriority.Character.Value, Runnn)
				end
			end)
		else
			Run:UnbindFromRenderStep("Runn")
		end
	end
end)

You can do a loop where every task.wait(0.1) it checks if the player is running and if they are add 1 to a value and if it reaches 20 make the player run, and just reset it to 0 if they stop

local Character = script.Parent
local Run = game:GetService("RunService")
local Humanoid:Humanoid? = Character:WaitForChild("Humanoid")
function Runnn()
	print("Running")
	Character.Humanoid.WalkSpeed = 32
end
Humanoid.Changed:Connect(function(Property)
	if Property == "MoveDirection" then
		if Humanoid.MoveDirection.Magnitude >= 1 then
			task.delay(2,function()
				if Humanoid.MoveDirection.Magnitude >= 1 then
					Run:BindToRenderStep("Runn", Enum.RenderPriority.Character.Value, Runnn)
				end
			end)
		else
	        Character.Humanoid.WalkSpeed = 16
			Run:UnbindFromRenderStep("Runn")
		end
	end
end)
1 Like

It worked! Thank You SO MUCH!!!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.