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

Input Keycode Animation Only Playing Under Random Circumstances. Any advice?

I have a few animations that play after a user presses a specific key. The problem is that only about half of them work after the player dies, and the 'roll' animation is only playable twice until it doesn't register the key at all. No errors show up in the output, what should I do?

Roll animation code:

wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false


UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.KeyCode == Enum.KeyCode.Q and debounce == false then
		debounce = true
		local Player = game.Players.LocalPlayer
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild("Humanoid")

		local Animation = Instance.new("Animation")
		Animation.AnimationId = "rbxassetid://123"

		local Track = Humanoid:LoadAnimation(Animation)
		Track:Play()
		local BodyV = Instance.new("BodyVelocity")
		BodyV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BodyV.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector * 25
		BodyV.Parent = Player.Character.HumanoidRootPart
		wait(1)
		BodyV:Destroy()
		Track.Stopped:Connect(function()
			attacking = false
			debounce = false
		end)
	else
		
    end
end)

Downward smash animation code: (works after player dies, infinite amount of times)

wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false


UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.KeyCode == Enum.KeyCode.E and debounce == false then
		local Player = game.Players.LocalPlayer
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild("Humanoid")
		if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
			debounce = true
			local Animation = Instance.new("Animation")
			Animation.AnimationId = "rbxassetid://456"
			local Track = Humanoid:LoadAnimation(Animation)
			Track:Play()
			Track.Stopped:Connect(function()
				attacking = false
				wait(3)
				debounce = false
			end)
		
		else
		
    	end
	end
end)

Flurry punch code: (works regularly before player dies, after that it's a simple roll of the dice)

wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false


UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.KeyCode == Enum.KeyCode.X and debounce == false then
		debounce = true
		local Player = game.Players.LocalPlayer
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild("Humanoid")

		local Animation = Instance.new("Animation")
		Animation.AnimationId = "rbxassetid://789"

		local Track = Humanoid:LoadAnimation(Animation)
		Track:Play()
		Track.Stopped:Connect(function()
			attacking = false
			wait(3)
			debounce = false
		end)
	else
		
    end
end)

Not asking for any new code, just asking for possible causes of this weird phenomenon and any edits I could potentially make. Thanks in advance, sorry if the code it a bit messy.

3 comments

0

^There's a couple more scripts but I didn't want to make it needlessly longer than it already was, everything included is in a separate localscript inside of starterplayerscripts

0

Use a PlayerAdded and CharacterAdded event, and place the script inside. It would fire everytime the player or character is added to the game.

0

@MArzal It's inside a LocalScript so I can't use playeradded. I'll check to see if characteradded works though

1 answer

✓ Accepted answer

This works.


local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false


UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.KeyCode == Enum.KeyCode.Q and debounce == false then
	print("Worked?")
        debounce = true
print(debounce)
print(attacking)
        local Player = game.Players.LocalPlayer
        local Character = Player.Character or Player.CharacterAdded:Wait()
        local Humanoid = Character:WaitForChild("Humanoid")

        local Animation = Instance.new("Animation")
        Animation.AnimationId = "rbxassetid://123"

        local Track = Humanoid:LoadAnimation(Animation)
        Track:Play()
        local BodyV = Instance.new("BodyVelocity")
        BodyV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
        BodyV.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector * 25
        BodyV.Parent = Player.Character.HumanoidRootPart
        wait(3)
        BodyV:Destroy()
debounce = false
        Track.Stopped:Connect(function()
            attacking = false
        end)
    else

    end
end)

I mean it might not line up with your animations and might not work with what you need, but I tried it and it works. All I really did was just took the debounce out of the track.Stopped, and removed the wait(-1).

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