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

[Solved] How to make a punch script work more than once?

I am trying to make an punch script for this game. I want the punch to change every time you click. For example I click 1 and it plays a right hand punch. But if I click 1 time then right after click another time I want it to play a left hand punch. This script I made does what I want except after you do it once it doesn't work again.

local player = game.Players.LocalPlayer
local db = true
local damaged = false
local UserInputService = game:GetService("UserInputService")
local combo = 0
local Punch1 = Instance.new("Animation")
Punch1.AnimationId = "http://www.roblox.com/asset/?id=5943464571"
local Punch2 = Instance.new("Animation")
Punch2.AnimationId ="http://www.roblox.com/asset/?id=5943982783"



game.Players.LocalPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not db and not damaged and hit.Parent.Humanoid ~= game.Players.LocalPlayer.Character.Humanoid then
		if game.Players.LocalPlayer.Character.Humanoid.Health > 0 then 
			damaged = true
			game.ReplicatedStorage.Punch:FireServer(hit.Parent.Humanoid)
		end
	end
end)

local function onInputBegan(input,gameProcessed)
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 and db and combo == 0 then 
		db = false 
		local PlayFirstPunch = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Punch1)
		PlayFirstPunch:Play()
		wait(0.8)
		damaged = false
		db = true
		combo = 1
		UserInputService.InputBegan:Connect(onInputBegan)
		
	elseif 
			input.UserInputType == Enum.UserInputType.MouseButton1 and db and combo == 1 then 
			db = false 
			local PlaySecondPunch = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Punch2)
			PlaySecondPunch:Play()
			wait(0.8)
			damaged = false
			db = true
			combo = 2
		
	end
end

UserInputService.InputBegan:Connect(onInputBegan)


1 answer

✓ Accepted answer

You put combo as 2 when the left punch is executed. This means that when the script went through both checks, combo was neither zero nor one. You have to set combo back to 0 on line 42 like so:

combo = 0

Hope this helped!

1 comment

0

Thanks!

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