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 fix my character animation script?

Hi, i recently made a script but it's not playing the animation on my character.

local Player = game.Players.LocalPlayer;
local AnimId = (248335946);

local URL = ("http://www.roblox.com/asset/?id=248335946");
local Anim = (URL..tostring(AnimId));
Player.CharacterAdded:connect(function(Char)
 local Hum = Char:WaitForChild("Humanoid");
 local Track = Hum:LoadAnimation(Anim)
 Track:Play();
 return nil;
end);

1 answer

Simple fix, you're concatanating the URL string that already has an asset Id with another asset Id, creating an invalid animation asset. Another thing is, while loading animations this way, we actually need the physical animation object to load. So we can just create with with Instance.new:

local Player = game.Players.LocalPlayer;
-- Got rid of the AnimId entirely
local Anim = "http://www.roblox.com/asset/?id=248335946" -- Already valid

Player.CharacterAdded:connect(function(Char)
	local Hum = Char:WaitForChild("Humanoid")
	local AnimObj = Instance.new("Animation",Hum)
	AnimObj.AnimationId = Anim
	local Track = Hum:LoadAnimation(AnimObj )
	Track:Play()
	AnimObj:Destroy()
	-- We also don't need that return value
end)

Or ...

local Player = game.Players.LocalPlayer
local AnimId = 248335946

local URL = "http://www.roblox.com/asset/?id=" -- Got rid of the Id here
local Anim = URL..tostring(AnimId)
Player.CharacterAdded:connect(function(Char)
	local Hum = Char:WaitForChild("Humanoid")
	local AnimObj = Instance.new("Animation",Hum)
	AnimObj.AnimationId = Anim
	local Track = Hum:LoadAnimation(AnimObj)
	Track:Play()
	AnimObj:Destroy()
end)

4 comments

0

Still dosen't work. :(

0

Oops, I forgot loading animations requires using the Animation object. I'll revise my answer.

0

Concatenation can be done without tostring (as the compiler converts automatically), and it can also be done directly. But, your choice, i guess.

0

With numbers, yes. But the concatenation process can only be preformed on the number as a variable, not a direct value. However, the same rules don't apply for other arbitrary values.

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