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

Why does this keep saying I'm trying to index a nil value when I try to change the animation Id?

So I am trying to change the default running animation and for some reason my code doesn't work

game.Players.PlayerAdded:Connect(function(player)
	playerCharacter = player.Character
		
	ourPlayer = player
	
	for i, v in pairs(playerCharacter.Humanoid:GetPlayingAnimationTracks()) do
		v:Stop(0)
	end
	
	local AnimateScript = playerCharacter:WaitForChild("Animate")
	for i, animation in pairs(AnimateScript:GetChildren()) do
		print("ANIMATION: ", animation.Name)
	end
	
	AnimateScript.run.Value = "Run"
	AnimateScript.run:FindFirstChild("RunAmin").AnimationId = "rbxassetid://<id>"

The value of run is changed but for some reason the last line of code here keeps giving me a "attempt to index a nil value". I honestly don't get why this is doesn't work, it seems so simple. All I want is to change the animation id. I know I could make a new Animation localscript but I really want this to work cause it would make things so much easier.

1 answer

I got it to work, but I honestly feel like it is so unnecessary but it works:

I deleted the stringvalue and the animation in it and remade it with my own animation id.

AnimateScript:WaitForChild("run"):Destroy()
	
local newRun = Instance.new("StringValue")
newRun.Name = "run"
newRun.Value = "run"
newRun.Parent = AnimateScript

local newAnimation = Instance.new("Animation")
newAnimation.AnimationId = "rbxassetid://<id>"
newAnimation.Name = "RunAnim"
newAnimation.Parent = newRun

And now the player runs with the animation I gave it.

1 comment

0

youre old script was findfirstchild("RunAmin") im going to geuss you made a spelling error while searching for the animation.

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