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

How would I make a script that only runs when the player is moving?

I have tried using RenderStepped but that still runs when the player is standing still

local RunService= game:GetService("RunService")

local Num = 0
RunService.RenderStepped:Connect(function()
	Num = Num+1
	print(Num)
end)

1 comment

0

UserInputService? ContextAction works too

1 answer

✓ Accepted answer

humanoid has a function called running:

local running = false

game.Workspace.Player.Humanoid.Running:Connect(function(speed) -- define humanoid
    if speed > 0 then
        running = true
	
	 -- code here(but make sure to constantly whether the player is running via the variable)
	
	
    else
        running = false
    end
end)

for ur instance since ur function is already a loop u can do this:

local running = false
local Num = 0

game.Workspace.Player.Humanoid.Running:Connect(function(speed)
    if speed > 0 then
        running = true
    else
        running = false
    end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if running then
		Num = Num + 1
		
		print(Num)
	end
end)

PS: im not merging the speed function with the renderStepped such that u can use it to check other things but u could also just combine it to loop check the humanoid speed

2 comments

0

In that case can't a state identifier work for ex. Humanoid.HumanoidStateType ...

0

Thank you it worked perfectly fine!

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