I’ve been doing some testing with programming animations. Basically, I have created an animation and when a player joins I want it to loop forever.
Here is my script:
local anim = script.Animation
wait(1)
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid --This is where the error is
local animDribbleRTrack = hum:LoadAnimation(anim)
while true do
wait(5)
animDribbleRTrack:Play()
end
I’ve tried to search the forum for a solution with no success.
I re-wrote your code and I believe it should work better! Let me know if it is still bugging around:
local Animation = script.Animation
task.wait(1)
local Players = game:GetService(“Players”)
local Player = Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:FindFirstChildOfClass(“Humanoid”)
local animDribbleRTrack = Humanoid:LoadAnimation(Animation)
while true do
task.wait(5)
animDribbleRTrack:Play()
end
local player = game.Players.LocalPlayer
local function onCharacterAdded()
--load and play animation
end
player.CharacterAdded:Connect(onCharacterAdded)
Update: I figured out that my character and my humanoid is actually found in the workspace when I join the game… So this is how I modified my code:
local anim = script.Animation
wait(1)
local player = game.Players.LocalPlayer
local char = game.Workspace.DMLGDPCC
local hum = char.Humanoid
local animDribbleRTrack = hum:LoadAnimation(anim)
while true do
wait(5)
animDribbleRTrack:Play()
end
V Workspace
With this, I successfully had my animation working! So here is the problem: how do I modify this code for it to work for every player in the game?
-- Get player
local player = game:GetService("Players").LocalPlayer
-- Wait for animation
local animation = script:WaitForChild("Animation")
-- Make this variable local
local animDribbleRTrack = nil
-- Make a function that takes a character and loads an animation on it
-- Then sets the animDribbleRTrack variable to be that animation
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animDribbleRTrack = animator:LoadAnimation(animation)
end
-- If there is already a character, then load the animation on it
if player.Character then
onCharacterAdded(player.Character)
end
-- When the player gets a new character, load the animation on it
player.CharacterAdded:Connect(onCharacterAdded)
-- Every 5 seconds, if there is an animation, play it
while true do
task.wait(5)
if animDribbleRTrack then
animDribbleRTrack:Play()
end
end