Help Understanding Animation Script

Hi,
I’m super new to the roblox platform and do not have a background in development. I’m working on my own game and am trying to learn through breaking down the things I see that already work in some of the pre-populated games when you launch studio. Specifically the walk-it simulator. I wanted to experiment with the animations in my own game but when I take the model and the script that I think is controlling the animation it doesn’t seem to work in my own game. Can anyone help me find out what I might be missing so I can actually start to mess around with animation?

Here’s the script:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- When the player gets closer than WAVE_THRESHOLD, NPC will wave.
-- Will not wave again until the player goes outside of WAVE_THRESHOLD again.
local WAVE_THRESHOLD = 20 -- studs
local WAVE_DEBOUNCE_TIME = 3 -- seconds

-- Variables
local player = Players.LocalPlayer
local character = script.Parent
local npcModel = workspace.LobbyAssets.NPC
local animationController = npcModel.AnimationController
local animation = Instance.new("Animation")
local lastWavedAt = 0
local characterIsNear = false
local waveAnimationTrack

-- Subtracting position from the CFrame leaves you with just the rotational elements of the CFrame
local rotationOffset = (npcModel.PrimaryPart.CFrame - npcModel.PrimaryPart.CFrame.Position):Inverse()
local Animations = {
	SuperheroIdle = 616111295,
	Wave = 507770239,
}
local NPC = {
	Waist = npcModel.UpperTorso.Waist,
	Root = npcModel.LowerTorso.Root,
	Neck = npcModel.Head.Neck,
}
local TwistProportions = {
	Root = .2,
	Waist = .3,
	Neck = .5,
	Full = 1,
}


-- Helper functions
local function loadAnimation(controller, animationId)
	animation.AnimationId = "rbxassetid://" .. animationId
	return controller:LoadAnimation(animation)
end

local function isolateAndDampenYAngle(targetCFrame, proportionMultiplier)
	proportionMultiplier = proportionMultiplier or 1
	local _, y = targetCFrame:ToEulerAnglesXYZ()
	
	return CFrame.new(targetCFrame.Position) * CFrame.Angles(
		0,
		y * proportionMultiplier,
		0
	)
end

local function makeNPCLookAt(lookAtPosition)
	if not lookAtPosition then
		return
	end
	
	local differenceVector = lookAtPosition - npcModel.PrimaryPart.Position
	local targetCFrame = rotationOffset * CFrame.new(Vector3.new(), differenceVector.Unit) or rotationOffset
	
	-- Let the waving animation move the body
	if not waveAnimationTrack.IsPlaying then
		NPC.Root.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Root)
		NPC.Waist.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Waist)
	end
	NPC.Neck.Transform = isolateAndDampenYAngle(targetCFrame, waveAnimationTrack.IsPlaying and TwistProportions.Full or TwistProportions.Neck)
	
	return differenceVector.Magnitude
end


-- Create and load the animations
loadAnimation(animationController, Animations.SuperheroIdle):Play()
waveAnimationTrack = loadAnimation(animationController, Animations.Wave)
waveAnimationTrack.Looped = false


RunService.Stepped:Connect(function()
	local distance = makeNPCLookAt(character.PrimaryPart.Position)
	if distance and distance < WAVE_THRESHOLD then
		if tick() - lastWavedAt > WAVE_DEBOUNCE_TIME and not characterIsNear then
			waveAnimationTrack:Play(.5)
			lastWavedAt = tick()
		end
		characterIsNear = true
	else
		characterIsNear = false
	end
end)

Here’s a screenshot of the model I’m trying to work with and how it’s setup:

Screen Shot 2021-12-06 at 9.00.14 PM

The reason this script does not work in your own game is because it was made specifically for the Walk-it simulator game.

If you want to learn about animating player characters, I suggest you start from the very basics.

Here are a few resources you should check out:

  1. Using the Animation Editor will teach you how to create your own animations using the Roblox Animation Editor.
  2. Using Animations in Game will teach you how to implement your created animations in a game setting. It goes over the basics of scripting animations into a character and understanding when and where to use them.
  3. Animation gives information regarding the animation instance’s properties, methods, and events.

I also suggest you search up a few YouTube videos about animation. There are many helpful videos out there from beginner to expert level.

Hope this helps.

Thanks for sharing those resources I will definitely take a look. Although I am still curious, what do you mean it was specifically made for the walk-it simulator game? Was it designed not to work outside of it? Asking because the primary way I learn is to isolate something that works the way I want, breaking it down, and rebuilding it. Which is what I’m trying to do here by my request above.

Wondering if it has something to do with the fact that you don’t own the animations provided in the script. You can’t use other people’s animations unless they are published into a free model or something like that. If you are using your own animations with this, then you should be fine so not really sure why it wouldn’t work.

Also wanna note that this entire script looks like it’s for an NPC detection system that detects when a user gets close enough, it’ll start playing animations.

Okay that makes sense I was wondering if maybe there was something specific to the platform that I might not know about. So using the links shared in the other post I can probably figure out how to create and publish animations to then experiment with this script?

1 Like

Exactly. The resources @xendatro provided are very helpful, and are the ones I used when starting to learn animations. Good luck!