Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Animation script not working after player dies and uses keybind mid-death. Potential solutions?

I have a localscript running that plays an animation after going through a few checks to make sure the animation isn't running unnecessarily -- However, when the player dies and they use the keybind for the animation to play while their joints are still broken and they're still waiting to be respawned, the animation no longer works. Am I doing the order wrong, or is it some alternative causative? I also have a server script that handles stamina which I'll also include.

Localscript:

local UserInputService = game:GetService("UserInputService")
local debounce = false
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://123"
local Track = Humanoid:LoadAnimation(Animation)
UserInputService.InputBegan:Connect(function(InputObject,istyping)
    if InputObject.KeyCode == Enum.KeyCode.E then
		if istyping == false then
			if debounce == false then
				if Player:WaitForChild("CanAttack").Value == true then
					if Character and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
						debounce = true
						game.ReplicatedStorage.ChangeStamina:FireServer(100)
        				Track:Play()
						Track.Stopped:Connect(function()
							debounce = false
						end)
					end
				else
					game.ReplicatedStorage.UpdateStamina:FireServer(100)
				end
			else
			end
		end
    end
end)
end)

Serverscript:

game.ReplicatedStorage.ChangeStamina.OnServerEvent:Connect(function(player, amt)
	if player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value >= amt then
		player:WaitForChild("CanAttack").Value = true
		player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value = player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value - amt
		player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar:TweenSize(UDim2.new(0,player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value,1,0),'In','Linear',0.5,true)
	else
		player:WaitForChild("CanAttack").Value = false
		player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value = 0
		player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar:TweenSize(UDim2.new(0,player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value,1,0),'In','Linear',0.5,true)
		local anims = player.Character.Humanoid:GetPlayingAnimationTracks()
		player.Character.Humanoid.WalkSpeed = 0
		for i,t in pairs(anims) do
			t:Stop()
		end
		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://123"
		local track = player.Character.Humanoid:LoadAnimation(animation)
		track:Play()
		player.Character.Humanoid.WalkSpeed = 0
		track.Stopped:Connect(function()
			player.Character.Humanoid.WalkSpeed = 11
		end)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		while true do
			wait(1)
			if player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value + 10 > 250 then
				player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value = 250
				player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar:TweenSize(UDim2.new(0,player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value,1,0),'In','Linear',0.5,true)
			else
				player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value = player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value + 10
				player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar:TweenSize(UDim2.new(0,player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value,1,0),'In','Linear',0.5,true)
			end
		end
	end)
end)

game.ReplicatedStorage.UpdateStamina.OnServerEvent:Connect(function(player,amt)
	if player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value >= amt then
		player.CanAttack.Value = true
	else
		player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar:TweenSize(UDim2.new(0,player:WaitForChild("PlayerGui").StaminaGui.RedBar.GreenBar.Stamina.Value,1,0),'In','Linear',0.5,true)
		player.CanAttack.Value = false
	end
end)

2 answers

✓ Accepted answer

Try putting it in startercharacterscripts, and see if that works. It should.

local UserInputService = game:GetService("UserInputService")
local debounce = false
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://123"
local Track = Humanoid:LoadAnimation(Animation)
UserInputService.InputBegan:Connect(function(InputObject,istyping)
    if InputObject.KeyCode == Enum.KeyCode.E then
        if istyping == false and debounce == false and Player:WaitForChild("CanAttack").Value == true and Character and Humanoid:GetState() == Enum.HumanoidStateType.Freefall and Humanoid.Health>0 then
                        debounce = true
                        game.ReplicatedStorage.ChangeStamina:FireServer(100)
                        Track:Play()
                        Track.Stopped:Connect(function()
                            debounce = false
                        end)
                else
                    game.ReplicatedStorage.UpdateStamina:FireServer(100)
                end
            else
            end
 
end)

Hello.

You just have to add a simple if statement checking if the humanoid's health is above 0, so they're not dead.

If that doesn't work, put it in StarterCharacterScripts.

local UserInputService = game:GetService("UserInputService")
local debounce = false
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://123"
local Track = Humanoid:LoadAnimation(Animation)
UserInputService.InputBegan:Connect(function(InputObject,istyping)
    if InputObject.KeyCode == Enum.KeyCode.E and Humanoid.Health > 0 then
        if istyping == false then
            if debounce == false then
                if Player:WaitForChild("CanAttack").Value == true then
                    if Character and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
                        debounce = true
                        game.ReplicatedStorage.ChangeStamina:FireServer(100)
                        Track:Play()
                        Track.Stopped:Connect(function()
                            debounce = false
                        end)
                    end
                else
                    game.ReplicatedStorage.UpdateStamina:FireServer(100)
                end
            else
            end
        end
    end
end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.