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

Check if a player is moving or not?

Hello.

I am currently working on a stamina script, and I have this glitch where if you start to run, the running animation starts, but if you stop without letting go of shift, it keeps playing. I have tried to switch up the places where the Animation plays, but I had no luck. I am unsure how to check if the player is actually running or not, after having pressed shift, and would like some help.

Here's my script:

local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild("StaminaBackground"):WaitForChild("Stamina")

local Player = game:GetService("Players").LocalPlayer

local NormalWalkSpeed = 13
local NewWalkSpeed = 23

local Power = 10
local IsWalking = false

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents

repeat wait() until Player.Character
repeat wait() until Player.Character.Humanoid

local Character = Player.Character
local Humanoid = Player.Character.Humanoid

local Animation = Humanoid.Animator:LoadAnimation(script.Run)

Player.Character.Humanoid.Running:Connect(function(Speed)
	IsWalking = (Speed ~= 0)
end)
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if not (IsWalking) then
			return;
		end
		Character.Humanoid.WalkSpeed = NewWalkSpeed
		IsSprinting = true
		Animation:Play()
		while Power > 0 and IsSprinting do
			Power = Power - 0.03
			Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
			wait()
			if Power <= 0 then
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				Animation:Stop()
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = NormalWalkSpeed
		IsSprinting = false
		Animation:Stop()
		while Power < 10 and not IsSprinting do
			Power = Power + 0.03
			Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
			wait()
			if Power <= 0 then
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				Animation:Stop()
			end
		end
	end
end)

If I could get some help with this, it would be great. Thanks!

1 answer

✓ Accepted answer

You would use BindableEvents for this, :Fire and .Event. Function on line 23 is exactly what will be needed, you just need to check if speed is 0 and if it is, then fire event that player has stopped, here is how it would look like:

Firing an Event

local PlayerStopped: BindableEvent = Instance.new('BindableEvent')

local IsWalking = false

Player.Character.Humanoid.Running:Connect(function(Speed)
	if (Speed == 0) then
		IsWalking = false
		
		--/This will fire a signal that player has stopped
		PlayerStopped:Fire()
	else
		IsWalking = true
	end
end)

This will fire a signal every time player's speed is 0, which means he stopped.

Catching the Event

Now obviously you need to catch the event because this just fires it for no reason, this is where that Event comes in handy. All you have to do is put the function that is inside of the InputEnded function into this function which means every time you fire PlayerStopped, it will run.

PlayerStopped.Event:Connect(function()
	Character.Humanoid.WalkSpeed = NormalWalkSpeed
	IsSprinting = false
	Animation:Stop()
	while Power < 10 and not IsSprinting do
		Power = Power + 0.03
		Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
wait()
if Power <= 0 then
Character.Humanoid.WalkSpeed = NormalWalkSpeed
Animation:Stop()
end
end
end)

Connecting it with InputEnded

Now we created the Event function which is same as InputEnded one... but that's the problem, duplicating code is something very bad you should never do so InputEnded function should be modified, in this case it will be calling PlayerStopped every time you release the LeftShift so it would look like this

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if (GameProcessedEvent) then
		return;
	end

	if (Input.KeyCode == Enum.KeyCode.LeftShift) then
		PlayerStopped:Fire()
	end
end)

I haven't opened studio yet and i wrote this in a website so i am not sure if there aren't any problems with that but i think you get the idea how you can solve your problem...

BindableEvents

1 comment

0

This worked very well. Thanks!

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