I want to animate an object, to add more RP In my game
The problem is that the animation work perfectly in-studio, but not in-game
I tried to edit my script, but it doesn’t solve the problem
here is the script:
Tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil
local hum = player.Character:WaitForChild("Humanoid")
local animation = Tool.Animation
local AnimTrack = hum:LoadAnimation(animation)
Tool.Activated:connect(function(mouse)
AnimTrack:play()
end)
As @Expertsm said that animations only work in game if you or group own the animations. Since I’m not a scripter, I think your script is working perfectly fine. Did you own the animations or group into your place? If it about the group, you have to export your animations to group creations otherwise it’ll never work in game.
Can’t be a problem of ownership. If OP didn’t own the animations, they wouldn’t be able to play them in Studio either. It purely won’t load in a game that isn’t the owner’s profile for security reasons. It could just be the way that this script is written.
Usually what I do is set character and humanoid variables in the equipped bit of the script rather than right at the top. I recognise that this is still a correct way of fetching the character but it feels better to just check the parent of the tool if I don’t need the player.
The one thing I would change, as is, is that repeat loop. Don’t use loops if there are events already available. In this case, you should be checking when CharacterAdded fires or if a character already exists, then work from there.
This is about how I’d write the code if I were working with this style:
local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
-- Required to prevent LoadAnimation from throwing an error
-- Will no longer be needed when character loading events order is fixed
while Humanoid and not Humanoid:IsDescendantOf(workspace) do
Humanoid.AncestryChanged:Wait()
end
local Animation = Tool.Animation
local AnimTrack = Humanoid:LoadAnimation(Animation)
Tool.Activated:Connect(function () -- Mouse is NOT passed, that's Equipped
AnimTrack:Play()
end)
Remember to check your console if your code doesn’t work just for any errors that you may come across. In Studio it’d be the Output, in game it would be the F9 Developer Console.
I tested your script, and it worked perfectly (I owned the animation with my profile, I also transferred the animation to my group), thank you for your help, I was lost C: @colbert2677