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

How do i make an animation play every time you move?

Im making a game where i want custom animations for example every time you walk i want an animation to play.

How do i make that animation to play every time you walk?

2 answers

First you need to detect when the player is moving, that can be done:

  1. Use the "Running" event on the Humanoid Instance of the Player in question: http://wiki.roblox.com/index.php?title=Running_(Event)

  2. Measure the velocity of the Torso of the Humanoid:

if character.Torso.Velocity.Magnitude > 2 then --[[They're walking!]]-- end

Change "2" to whatever is a suitable threshold for your scenario

  1. Track the movement keys using the KeyDown event on the PlayerMouse

Then you will need to trigger your animation, but exact code for this depends on your own setup. You might be using welds, plain CFrames, Roblox Animations, etc..

Hope this helps!

insert a script and add

Anim = script.Animator
function Figure:insert(fig)
	if fig:FindFirstChild("Animate") then fig.Animate:Remove() end
	if not fig:FindFirstChild("Animator") then 
		a = Anim:Clone()
		a.Disabled = false
		a.Parent = fig 
	end
	for i, v in pairs(fig:GetChildren()) do
		if v:IsA("CharacterMesh") then v:Remove() end
	end
	local Cm1 = Instance.new("CharacterMesh")
	Cm1.Name = "Torso"
	Cm1.MeshId = "27111894"
	Cm1.BodyPart = "Torso"
	Cm1.Parent = fig
	local Cm2 = Instance.new("CharacterMesh")
	Cm2.Name = "Right Arm"
	Cm2.MeshId = "27111864"
	Cm2.BodyPart = "RightArm"
	Cm2.Parent = fig
	local Cm3 = Instance.new("CharacterMesh")
	Cm3.Name = "Left Arm"
	Cm3.MeshId = "27111419"
	Cm3.BodyPart = "LeftArm"
	Cm3.Parent = fig
	local Cm4 = Instance.new("CharacterMesh")
	Cm4.Name = "Left Leg"
	Cm4.MeshId = "27111857"
	Cm4.BodyPart = "LeftLeg"
	Cm4.Parent = fig
	local Cm5 = Instance.new("CharacterMesh")
	Cm5.Name = "Right Leg"
	Cm5.MeshId = "27111882"
	Cm5.BodyPart = "RightLeg"
	Cm5.Parent = fig
end 

while true do wait()
	for i, v in pairs(game.Players:GetChildren()) do
		if v.Character then
			Figure:insert(v.Character)
			if game:FindFirstChild("NetworkServer") then
				PGui:insert(v.PlayerGui)
			end
		end
	end
end 

and then add a localscript into that script and name the localscript Animator and type

function waitForChild(parent, childName)
	local child = parent:findFirstChild(childName)
	if child then return child end
	while true do
		child = parent.ChildAdded:wait()
		if child.Name==childName then return child end
	end
end
local Figure = script.Parent
local Torso = waitForChild(Figure, "Torso")
local RightShoulder = waitForChild(Torso, "Right Shoulder")
local LeftShoulder = waitForChild(Torso, "Left Shoulder")
local RightHip = waitForChild(Torso, "Right Hip")
local LeftHip = waitForChild(Torso, "Left Hip")
local Neck = waitForChild(Torso, "Neck")
local Humanoid = waitForChild(Figure, "Humanoid")
local pose = "Standing"

if Figure:FindFirstChild("View") then Figure.View:Remove() end
H = Instance.new("Hat")
P = Instance.new("Part")
H.Name = "View"
P.Parent = H
P.Position = Figure.Head.Position
P.Name = "Handle" 
P.formFactor = 0
P.Size = Vector3.new(1, 1, 1) 
P.BottomSurface = 0 
P.TopSurface = 0 
P.Locked = true 
P.Transparency = 1
H.Parent = Figure
H.AttachmentPos = Vector3.new(0, 0.75, 0.5)
H.AttachmentRight = Vector3.new(1, 0, 0)
H.AttachmentUp = Vector3.new(0, 1, 0)
H.AttachmentForward = Vector3.new(-0, -0, -1)

--[[local Cam = game.Workspace.CurrentCamera
while true do wait()
	if (Cam.focus.p - Cam.CoordinateFrame.p).magnitude > 0.5 then
	CF = Cam.focus * CFrame.new(0,0,1)
	Cam.CoordinateFrame = CFrame.new(CF.p)
	end
end ]]

Humanoid.WalkSpeed = 6
workspace.CurrentCamera.CameraType = "Follow"
workspace.CurrentCamera.CameraSubject = P

local toolAnim = "None"
local toolAnimTime = 0

-- functions

function onRunning(speed)
	if speed>0 then
		pose = "Running"
	else
		pose = "Standing"
	end
end

function onDied()
	pose = "Dead"
end

function onJumping()
	pose = "Jumping"
end

function onClimbing()
	pose = "Climbing"
end

function onGettingUp()
	pose = "GettingUp"
end

function onFreeFall()
	pose = "FreeFall"
end

function onFallingDown()
	pose = "FallingDown"
end

function onSeated()
	pose = "Seated"
end

function onPlatformStanding()
	pose = "PlatformStanding"
end

function moveJump()
	RightShoulder.MaxVelocity = 0.5
	LeftShoulder.MaxVelocity = 0.5
	RightShoulder.DesiredAngle = 3.14
	LeftShoulder.DesiredAngle = -3.14
	RightHip.DesiredAngle = 0
	LeftHip.DesiredAngle = 0
end


-- same as jump for now

function moveFreeFall()
	RightShoulder.MaxVelocity = 0.5
	LeftShoulder.MaxVelocity = 0.5
	RightShoulder.DesiredAngle = 3.14
	LeftShoulder.DesiredAngle = -3.14
	RightHip.DesiredAngle = 0
	LeftHip.DesiredAngle = 0
end

function moveSit()
	RightShoulder.MaxVelocity = 0.15
	LeftShoulder.MaxVelocity = 0.15
	RightShoulder.DesiredAngle = 3.14 /2
	LeftShoulder.DesiredAngle = -3.14 /2
	RightHip.DesiredAngle = 3.14 /2
	LeftHip.DesiredAngle = -3.14 /2
end

function getTool()	
	for _, kid in ipairs(Figure:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end

function getToolAnim(tool)
	for _, c in ipairs(tool:GetChildren()) do
		if c.Name == "toolanim" and c.className == "StringValue" then
			return c
		end
	end
	return nil
end

function animateTool()
	
	if (toolAnim == "None") then
		RightShoulder.DesiredAngle = 1.57
		return
	end

	if (toolAnim == "Slash") then
		RightShoulder.MaxVelocity = 0.5
		RightShoulder.DesiredAngle = 0
		return
	end

	if (toolAnim == "Lunge") then
		RightShoulder.MaxVelocity = 0.5
		LeftShoulder.MaxVelocity = 0.5
		RightHip.MaxVelocity = 0.5
		LeftHip.MaxVelocity = 0.5
		RightShoulder.DesiredAngle = 1.57
		LeftShoulder.DesiredAngle = 1.0
		RightHip.DesiredAngle = 1.57
		LeftHip.DesiredAngle = 1.0
		return
	end
end

function move(time)
	local amplitude
	local frequency
  
	if (pose == "Jumping") then
		moveJump()
		return
	end

	if (pose == "FreeFall") then
		moveFreeFall()
		return
	end
 
	if (pose == "Seated") then
		moveSit()
		return
	end

	local climbFudge = 0
	
	if (pose == "Running") then
		RightShoulder.MaxVelocity = 0.15
		LeftShoulder.MaxVelocity = 0.15
		amplitude = 1
		frequency = 9
	elseif (pose == "Climbing") then
		RightShoulder.MaxVelocity = 0.5 
		LeftShoulder.MaxVelocity = 0.5
		amplitude = 1
		frequency = 9
		climbFudge = 3.14
	else
		amplitude = .2
		frequency = 1
	end

	desiredAngle = amplitude * math.sin(time*frequency)

	RightShoulder.DesiredAngle = (desiredAngle + climbFudge)/5
	LeftShoulder.DesiredAngle = (desiredAngle - climbFudge)/5
	RightHip.DesiredAngle = -desiredAngle/3
	LeftHip.DesiredAngle = -desiredAngle/3
	Neck.DesiredAngle = desiredAngle/8


	local tool = getTool()

	if tool then
	
		animStringValueObject = getToolAnim(tool)

		if animStringValueObject then
			toolAnim = animStringValueObject.Value
			-- message recieved, delete StringValue
			animStringValueObject.Parent = nil
			toolAnimTime = time + .3
		end

		if time > toolAnimTime then
			toolAnimTime = 0
			toolAnim = "None"
		end

		animateTool()

		
	else
		toolAnim = "None"
		toolAnimTime = 0
	end
end
Humanoid.Died:connect(onDied)
Humanoid.Running:connect(onRunning)
Humanoid.Jumping:connect(onJumping)
Humanoid.Climbing:connect(onClimbing)
Humanoid.GettingUp:connect(onGettingUp)
Humanoid.FreeFalling:connect(onFreeFall)
Humanoid.FallingDown:connect(onFallingDown)
Humanoid.Seated:connect(onSeated)
Humanoid.PlatformStanding:connect(onPlatformStanding)
local runService = game:service("RunService");

while Figure.Parent~=nil do
	local _, time = wait(0.1)
	move(time)
end

and that's all hope it helped but on scripts you must use cframe positions to make the animation

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