Hi there! I made an animation for an R15 rig which I’d like any player ingame to be able to execute upon resetting or dying. Was wondering if anyone knew how to script that and additionally make it so the full length of the animation is able to play before the player respawns. It’s only about 1:40 seconds long.
(I can’t script so please explain it in basic terms for me if you can thanks lol)
local Players = game:GetService("Players")
--// Initialization//--
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
-- Wait for the player to join the game
Players.PlayerAdded:Connect(function(player)
-- Runs the code inside and hooks the effect whenever the character spawns again
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- Disable body parts "exploding" upon death to properly play the animation
humanoid.BreakJointsOnDeath = false
-- Creates the animation
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://AnimationIdHere"
local animationTrack = animator:LoadAnimation(animation)
-- Listens for the death of the humanoid
local diedConnection
diedConnection = humanoid.Died:Connect(function()
diedConnection:Disconnect() -- Prevent multi-running/Potential memory leaks
animationTrack:Play() -- Plays the animation
animationTrack.Stopped:Wait() -- Waits for the animation to stop
animationTrack:Destroy() -- Destroys both animation and track for potential memory leaks
animation:Destroy()
player:LoadCharacter() -- Spawns character
end)
end)
end)
local Players = game:GetService("Players")
--// Initialization//--
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
-- Wait for the player to join the game
Players.PlayerAdded:Connect(function(player)
-- Runs the code inside and hooks the effect whenever the character spawns again
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- Wait until humanoid exists on the workspace/player character
repeat
wait()
until humanoid.Parent == character
-- Disable body parts "exploding" upon death to properly play the animation
humanoid.BreakJointsOnDeath = false
-- Creates the animation
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://AnimationIdHere"
local animationTrack = animator:LoadAnimation(animation)
-- Listens for the death of the humanoid
local diedConnection
diedConnection = humanoid.Died:Connect(function()
diedConnection:Disconnect() -- Prevent multi-running/Potential memory leaks
animationTrack:Play() -- Plays the animation
animationTrack.Stopped:Wait() -- Waits for the animation to stop
animationTrack:Destroy() -- Destroys both animation and track for potential memory leaks
animation:Destroy()
player:LoadCharacter() -- Spawns character
end)
end)
-- Loads the character for the first time
player:LoadCharacter()
end)
2 changes: Because of the behavior of character spawning, we have to wait until the Humanoid exists within the physical realm first.
After creating the CharacterAdded event (very important to do this first), we spawn the character for the first time (which I completely forgot to do )
Thanks for the fix haha. The animation executes upon resetting but it looks like gravity or some other external factor pulls the character backwards so I fall over the wrong way while the animation is playing.
This won’t work like you think it does. If character does not have a HumanoidRootPart, trying to index character for it will error. Use FindFirstChild if you want to check whether something exists or not.
Yeah, another mistake on my end. Typically, I would just group it up with the humanoid and animator variables… not to mention I’ve been working heavily with tables for the past month so the validity of direct indexing totally messed with me haha.
@nd89 I would replace the HumanoidRootPart code with this:
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
humanoidRootPart.Anchored = true
end