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

Putting multiple animations on one key. Help?

So, I'm trying to make this script play multiple animations. The output says nothing is wrong, but it won't work. For reference, the game is in FE.

local debounce = false 
local leftp = false
local Character = game.Players.LocalPlayer.Character

function onKeyPress(inputObject, gameProcessedEvent) 
    if not debounce then 
        if inputObject.KeyCode == Enum.KeyCode.Q then
            debounce = true 
            leftp = not(leftp)
end
end
end
if leftp then           
  local animation = script.LPunch
    local anim = Character.Humanoid:LoadAnimation(animation)
    anim:Play()
    debounce = true
    wait(0.3)
   debounce = false

else

     local animation2 = script.RPunch
            local anim2 = Character.Humanoid:LoadAnimation(animation2)
            anim2:Play() 
    debounce = true
    wait(0.3)
    debounce = false
end



game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment

0

What does it do?

1 answer

✓ Accepted answer

Hey TheCrimsonVortex,

I understand the issue in your code is. You check for the if statement outside of the function, which makes the code run only once. You need to either check for the if statement inside the function, or make a while loop on the outside of the function that checks the statements. I recommend just checking the statements inside the function, because it's much more efficient than running a while loop in the background constantly.

###Here is how I did it, and I will provide you with a line-by-line explanation of the script, unless it's obvious.

####Example with description:

local uis = game:GetService("UserInputService");
local plrs = game:GetService("Players");
local cool_down, anim = true, false -- cool_down is the debounce of this function, and anim is the switch of this function. When anim is false, one statement runs and when anim is true another statement runs.
local t = 1 -- The amount of time that it's allowed to cool down.

uis.InputBegan:Connect(function(obj, gp) -- Anonymous function with obj and gp as parameters.
	if obj.KeyCode == Enum.KeyCode.Q and not gp then -- Checks if the character pressed 'Q' and if he/she did it while typing or not.
		if cool_down then
			cool_down = false; 
			
			if anim then -- Checks if anim is true, and if so runs this statement
				-- BLah blah.
				print("Play anim 1.");
			else -- If anim is false, it will run the statement below.
				-- Blah blah.
				print("Play anim 2.");
			end
			
			anim = not anim; -- Makes anim opposite to its previous value.
			
			wait(t); -- Wait for cool down.
			cool_down = true; -- Sets cool_down back to true in order to let the function run again.
		end
	end
end)

##Well, I hope I helped and have a nice day/night.

###~~ KingLoneCat

6 comments

0

Thank you, but the cooldown doesn't seem to work for some reason?

0

It does work. It just waits 1 second for cool down. If you wanna change the amount of time it waits, just change t.

0

I have and it didn't work.

0

I even tried making a really long cooldown and it would just happen fast still.

0

I accepted your answer, but I'd really appreciate if the cooldown would work.

0

It works on my Studio. Idk why it's not working on urs.

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