Problems with random animations for an npc

  1. What do you want to achieve?
    I’m trying to make it so that each time player joins the game, there will be a chance of an new anim appearing for the npc instead of the main one.

  2. What is the issue?
    Everything works properly as planned (first anim works too), but last two anims, they for some reason show on the server only, not on the player’s screen

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking on the developer forum but didn’t found anything.
local animation = Instance.new("Animation")
animation.Parent = script

local random = math.random(1, 3)
task.wait(0.1)
print(random)
task.wait(0.1)
if random == 1 then
	animation.AnimationId = "rbxassetid://9847473852"
	local anim = game.Workspace.Dummy.Humanoid:LoadAnimation(animation)
	anim.Looped = true
	anim:Play()
elseif random == 2 then
	animation.AnimationId = "rbxassetid://10321235458"
		local anim = game.Workspace.Dummy.Humanoid:LoadAnimation(animation)
		anim.Looped = true
	anim:Play()
elseif random == 3 then
	animation.AnimationId = "rbxassetid://10294280807"
	local anim = game.Workspace.Dummy.Humanoid:LoadAnimation(animation)
	anim.Looped = true
	anim:Play()
end

Try using Player.CharacterAdded maybe.

Do you want the animation to be randomized for every player in the game or the same for every player in the game?

It’s a single player game, sorry forgot to say that. The main problem is for some reason when i test it animation doesn’t show on mine screen, but pretty much does on the current server

Do i do something like Player.CharacterAdded:Wait()?

If this is a one player game you should use a local script.


local animation = Instance.new("Animation")
animation.Parent = script

local function Animate(ID)
animation.AnimationId = ID
local anim = workspace.Dummy:LoadAnimation(animation)
anim.Looped = true
anim:Play()
end


local random = math.random(1, 3)
task.wait(0.1)
print(random)
task.wait(0.1)
if random == 1 then
Animate("rbxassetid://9847473852")

elseif random == 2 then	

Animate("rbxassetid://10321235458")

elseif random == 3 then
Animate("rbxassetid://10294280807")
end

If that’s the case, then handle the animations locally.

Put this inside a local script in StarterGui:

repeat
	task.wait()
until nil ~= workspace.Dummy.Humanoid

local hum = workspace.Dummy.Humanoid
local animation = Instance.new("Animation")

local animTable = {
	[1] = "rbxassetid://9847473852";
	[2] = "rbxassetid://10321235458";
	[3] = "rbxassetid://10294280807";	
}

animation.AnimationId = animTable[math.random(1,#animTable)]
local finishedAnim = hum:LoadAnimation(animation)

local function playAnim()
	finishedAnim:Play()
	finishedAnim.Stopped:Connect(function()
		playAnim()
	end)
end
playAnim()
1 Like

still doesn’t work, might be animations itself problem. Because im out of ideas why it won’t work

If you use my script, make sure you correctly fix the paths to the dummy on lines 3 and 5.

Oh! It works now! Thanks, i would probably come to that fix only after some hours of melting my brains, I’m very grateful to you!

1 Like

Absolutely. Have a good one. :)