Help finding a characters speed

I know this has been asked alot but I cant find an answer that works. Im trying to make it so when a player is not moving they have an idle animation play. same for walking and running. but i cant figure out how to get a players speed and if theyre moving at all. I got a script from someone which im editing to make it so the walking running and idle running anims play. I hate coding animations.

the local script is in the gun

I think the main problem here is how you set up your animation priorities. For instances, idle animations’ priority should be set to idle. While for walking and running, you should set their animation priority to movement. Hope this helps.

humanoid.Running:Connect(function(speed)
	print(speed)
end)
2 Likes

Ive tried that but I get this error

Can you paste the relevant part of the script here instead of a screenshot?

1 Like

local humanoid = script:FindFirstAncestorOfClass(“Model”)
humanoid.Running:Connect(function(speed)
print(speed)
end)

local connection

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		if humanoid.Health > 0 then
			connection = humanoid.Running:Connect(function(speed)
				print(speed)
			end)
		end
	end
end)

tool.Unequipped:Connect(function()
	if connection then
		if connection.Connected then
			connection:Disconnect()
		end
	end
end)
2 Likes

thanks it worked! what is the local connection?

connection is declared outside of the two connections so that it’s within the scope of those two callback functions. Its purpose is so that if/when the tool is unequipped the humanoid.Running connection is disconnected, this is to prevent memory leaks. If however the connection wasn’t disconnected each time the tool were to be equipped the humanoid.Running connection would be created.

1 Like