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

Having a problem with Humanoid.Running:Connect(function(player)?

I want walking animation to stop when player is not moving, i tryed if speed == 0 but doesnt works, thanks.

local Player = game.Players.LocalPlayer
local Tool = script.Parent

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Stance1Anim = game.ReplicatedFirst.Animations.Stances.DefStance
local Stance1MoveAnim = game.ReplicatedFirst.Animations.Stances.DefStanceMoving


local Stance1 = Humanoid:LoadAnimation(Stance1Anim)
local Stance1Move = Humanoid:LoadAnimation(Stance1MoveAnim)
local EquippedVal = script.Eq
EquippedVal = false

script.Parent.Equipped:connect(function(player)	
	Stance1:Play()
	EquippedVal = true
end)
Humanoid.Running:Connect(function(speed)
	if EquippedVal == true then
	Stance1Move:Play()
	end
end)

script.Parent.Unequipped:connect(function()
	Stance1:Stop()
	Stance1Move:Stop()
	EquippedVal = false
end)

1 answer

You can check the velocity of the HumanoidRootPart of the character. If it's higher than 1 then the humanoid is running/not idling. If it's lower, then it's idling. We will listen to the humanoid state and whenever it get fired, we connect it to our function. This method doesn't need us to check if the character is freefalling or jumping since if they are, their velocity.Magnitude would be higher than 1. Here's how:

local tool = script.Parent
-- Services
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Player = game:GetService("Players")
-- Getting player variables
local LocalPlayer = Player.LocalPlayer
-- Getting char and humRootPart of player
local char = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local humRootPart = char:WaitForChild("HumanoidRootPart")
-- Folders
local animationsFolder = ReplicatedFirst:FindFirstChild("Animations")
local stancesFolder = animationsFolder:FindFirstChild("Stances")
-- Variables
local isIdling = true
local toolIsActive = false
-- Animations
local stance1Anim = stancesFolder:FindFirstChild("DefStance")
local stance1MoveAnim = stancesFolder:FindFirstChild("DefStanceMoving")
-- Loading animations
local stance1 = hum:LoadAnimation(stance1Anim)
local stance1Move = hum:LoadAnimation(stance1MoveAnim)
-- Functions
local function StopAllAnimations(hum)
	for _, playingTracks in pairs(hum:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end
end

tool.Equipped:Connect(function()
	toolIsActive = true
	stance1:Play()
end)

tool.Unequipped:Connect(function()
	toolIsActive = false
	StopAllAnimations(hum)
end)

local function GetCharState()
	if toolIsActive then
		if humRootPart.Velocity.Magnitude >= 1 then
			if isIdling then
				isIdling = false
				stance1Move:Play()
			end
		else
			if not isIdling then
				isIdling = true
				StopAllAnimations(hum)
			end
		end
	end
end

hum.Running:Connect(GetCharState)

2 comments

0

i hadto change some stuff but in the end it worked thanks <3

0

Awesome. Don't forget to mark the question as answered :p

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