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

I need help on a script that will make three punches throw consecutively . Could anyone help?

I have three different punching animations called animation1 animation2 and animation3 in the script. I need someones help trying to allow the** script to do one punch then press f again in a period of like 0.5 seconds** (after the first one finished)** to activate a second one and then another one in another period of 0.5 seconds after the second one finished**. I was thinking else return might work but it didn't. Also, if they dont press f inbetween after the other one finished then it to cancel out. Also i** dont want it to keep restarting the punch by spamming f**. I know this sounds a lot but this is out of my area of thinking. Any help would be appreciated. Thanks.



CanDoDmg = true -- If the player can do damage
Keybind1 = "f"
Keybind2 = "f"
Keybind3 = "f" -- Keybind to activate the punch
dmg = 20 -- How much damage the player takes


local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key)
	if key == Keybind1 then
		local anim = script.Parent.Humanoid:LoadAnimation(script.Animation1)
		anim:Play() 
		wait()
		script.Parent.RightHand.Touched:connect(function(hit)
			if hit.Parent.Humanoid and CanDoDmg == true then
			hit.Parent.Humanoid:TakeDamage(dmg)
			CanDoDmg = false
			wait(1)
				CanDoDmg = true
				
				
mouse.KeyDown:Connect(function(key)
	if key == Keybind2 then 
		local anim = script.Parent.Humanoid:LoadAnimation(script.Animation2)
		anim:Play() 
		wait()
		script.Parent.RightHand.Touched:connect(function(hit)
			if hit.Parent.Humanoid and CanDoDmg == true then
			hit.Parent.Humanoid:TakeDamage(dmg)
			CanDoDmg = false
			wait(1)
				CanDoDmg = true				
								
mouse.KeyDown:Connect(function(key)
	if key == Keybind3 then
		local anim = script.Parent.Humanoid:LoadAnimation(script.Animation3)
		anim:Play() 
		wait()
		script.Parent.RightHand.Touched:connect(function(hit)
			if hit.Parent.Humanoid and CanDoDmg == true then
			hit.Parent.Humanoid:TakeDamage(dmg)
			CanDoDmg = false
			wait(1)
				CanDoDmg = true				
								
						
								
								
								

			end
		end)
	end
end)

1 answer

You can encode the punch level as a numeric variable and check it like so:

local CanDoDmg = true -- If the player can do damage
local Keybind = "f"
local damage = 20 -- How much damage the player takes

local punch_level = 0
local punch_animations = {script.Animation1, script.Animation2, script.Animation3}

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key)
    if key == Keybind then
        if punch_level < 3 then
	    punch_level += 1 -- increase punch level, unless it's already the maximum
        else
            -- Abort the punch, because we don't have a 4th animation.
            -- If you'd like it do something else, please say so in a comment.
            return
        end
        local anim = script.Parent.Humanoid:LoadAnimation(punch_animations[punch_level])
        anim:Play()

        -- Removed a wait() here

        script.Parent.RightHand.Touched:connect(function(hit)
            if hit.Parent.Humanoid and CanDoDmg == true then
                hit.Parent.Humanoid:TakeDamage(dmg)
                CanDoDmg = false
                wait(1)
                CanDoDmg = true
            end
        end)

        local saved_punch_level = punch_level
        wait(0.5)
        if punch_level == saved_punch_level then
            -- This code is only executed if the player hasn't initiated another punch.
            punch_level = 0
        end
    end
end)

To go through the logic:

  • Every time the player punches, the punch level is increased (unless the level is already the maximum).
  • After 0.5 seconds, the punch level is reset to 0, unless the player has initiated another punch (in which case it isn't changed).
  • The initiation of another punch is detected by checking if the current punch_level is the same as it was before waiting.

2 comments

0

Oh, and you might also want to do something about the Touched event that is connected to the right hand. Right now it can stack, so the fifth time you punch somebody, it will deal 100 damage.

0

Hey thanks for the script it all seems good in the code yet when i run it, it says Animation1 is not a valid member of script yet it is in the script? This might be a roblox error as nothing appears wrong in your script and there are no red bits. Ill have another look into it

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