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

Playing Random Animations?

I'm trying to make random animations every time someone equips a tool in local script but it's not working, any idea whats wrong with this?

Edit: fixed it, is this good?

local animations = {"127487", "242579", "189476", "247846"}

function test()
animation = Instance.new("Animation",  script.Parent.Parent.Parent)
animation.AnimationID = "http://www.roblox.com/asset?id="..animations[math.random.(1, #animations)]"
AT = script.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)
AT:Play()
end
end

script.Parent.Equipped:connect(test)

1 answer

✓ Accepted answer

Your first and most obvious problem is that you left out an end that must close all functions. Function format:

function funcName(parameters)
	--code
end

Second, you try to use math.random, but it's inside a string! A string is just a collection of characters -- it doesn't tell the computer do to anything. Therefore, any code you have as a string will not be executed. You also did the link wrong -- there should be no slash after asset. You also have an unneeded dot by math.random.

Correct concatenation:

"www.roblox.com/asset?id="..math.random(1, #animations)

--You may need the http in the front.
"http://www.roblox.com/asset?id="..math.random(1, #animations)

However, this still won't work. math.random returns a number, so after all the code is read you'll end up with something like,

"http://www.roblox.com/asset?id=2"

which is not what you want. What we need to do is get the value from the table that corresponds with the random number.

animations[math.random(1, #animations)]

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