So I want to make it so that when I fire my gun it uses random animation, but this function seems to not be working
local function randomizeanim()
-- Define a table of animation IDs
local animationIDs = {
"rbxassetid://77860279409935",
"rbxassetid://130032324938878",
}
-- Ensure there are animations to choose from
if #animationIDs == 0 then
warn("No animations found in the animation table")
return
end
-- Initialize a variable to store the selected animation ID
local randomAnimID
repeat
-- Select a random animation ID
randomAnimID = animationIDs[math.random(1, #animationIDs)]
until randomAnimID ~= tool.Animations.Shoot.AnimationId
-- Update the animation ID
tool.Animations.Shoot.AnimationId = randomAnimID
-- Debug logs to verify the change
print("New Animation ID:", randomAnimID)
print("Updated Shoot Animation ID:", tool.Animations.Shoot.AnimationId)
end
I see the issue here.
To play an animation, you must load it into an animator. I assume you loaded the first animation, but not any further ones. Changing the ID of the Shoot animation object will not change the animation that’s loaded onto the animator.
What you could do instead is first load each animation into its own variable, and then into a table. You can then have the function pick one of the variables (which holds the reference to the loaded animations) and pass it. Then outside the function you can call :Play() on it.
Let me know whether that solves your problem.
I have to go, so if you need a code example I can provide it in a few hours.