How to add a part to my game with a script

Hello, I’m trying to play an animation on the server. For some reason this isnt working. Here’s the code:

for i,v in pairs(game.Players:GetPlayers()) do
		local animator = Instance.new("Animator")
		animator.Parent = v.Character.Humanoid
		
		local PooryAnimation = Instance.new("Animation")
		PooryAnimation.Parent = v.Character.Humanoid
		PooryAnimation.AnimationId = "rbxassetid://11640426584"
		
		local LoadedPoorAnimation = animator:LoadAnimation(PooryAnimation)
		LoadedPoorAnimation:Play()
		LoadedPoorAnimation.Looped = true
end

It has no errors and I tried making breakpoint but it just went through without any issues

1 Like

there are a few problems with this script but ill make it simple and it should be easy to follow. You dont need another animator in the character unless you make a “StarterCharacter” without it, but im guessing thats not the case. Another thing is that you can use the humanoid to animate.

so what you should do is:

for i,v in pairs(game.Players:GetPlayers()) do
		local player = v.Character -- Define the character
		local Hum = player:WaitForChild("Humanoid") -- get the humanoid

		local PooryAnimation = Instance.new("Animation")
		PooryAnimation.Parent = v.Character -- load the animation into the character not the humanoid
		PooryAnimation.AnimationId = "rbxassetid://11640426584"
		
		local LoadedPoorAnimation = animator:LoadAnimation(PooryAnimation)
        LoadedPoorAnimation.Looped = true
		LoadedPoorAnimation:Play()
end

This should work, your only errors were creating a new animator(there is already an animator under the humanoid for future reference) and putting the animation in a weird spot. If you have questions or something is wrong just let me know.

This code is indeed wrong
let me show you the fixed one

game:GetService("Players").PlayerAdded:Connect(function(player)
local function oncharadd()
local PooryAnimation = Instance.new("Animation")
PooryAnimation.AnimationId = "rbxassetid://11640426584"
local LoadedPoorAnimation = player.Character:WaitForChild("Humanoid"):LoadAnimation(PooryAnimation)
LoadedPoorAnimation:Play()
LoadedPoorAnimation.Looped = true
end
if player.Character then oncharadd() end
player.CharacterAdded:Connect(oncharadd)
end)

Make a normal script / server script then put this in serverscriptservice

wouldn’t work since when you getplayers, only the 1st player who joined will be registered inside the getplayers. also won’t get the character since it is nil and the serverscriptservice just had started when the player joins a game. PlayerAdded is more efficient

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.