Punch Script found 2 problems while testing?

So, I was testing the game with my friend and I found some bugs during the testing…
Here are 2 bugs that we found while testing:

  • Randomly, when you punch someone or crit someone (spacebar and lmb) you get certain damages like 90,80,40 and even 50, while its supposed to be 20 or 10.
  • It hits the player who attacked even after all the checks??

here is the script:

local Debounce = false
local Debounce2 = false
local Combo = 1
local crit = false
local hitalready = false
local punching = false

--Create Animation--
local animation = script.Animation
local animation2 = script.Animation2

game.ReplicatedStorage.CritEvent.OnServerEvent:Connect(function(player)
	crit = true
	wait(1)
	crit = false
end)

game.ReplicatedStorage.PunchEvent.OnServerEvent:Connect(function(player, mouse)
	punching = true
				if Debounce == false then

		local character = player.Character
		
		if Combo == 1 then
		local animationTrack = character.Humanoid:LoadAnimation(animation)

			animationTrack:Play()
			
			Debounce = true
		

			wait(1)
			Combo = 2
			animationTrack:Stop()
	elseif Combo == 2 then
			local animationTrack = character.Humanoid:LoadAnimation(animation2)
			animationTrack:Play()

			Debounce = true


			wait(1)
			Combo = 1
			animationTrack:Stop()
		end

		Debounce = false

		character.LeftHand.Touched:Connect(function(touch)
			if Debounce2 == false and touch.Parent:FindFirstChild("Humanoid") and hitalready == false and punching == true and touch.Parent ~= character and touch.Parent.Name ~= character.Name then
				if crit == true and hitalready == false and punching == true and touch.Parent ~= character  then
					touch.Parent.Humanoid.Health = touch.Parent.Humanoid.Health - 20
					print("crit1")
					local Tagged = Instance.new("ObjectValue")
					Tagged.Name = "creator"
					Tagged.Value = player
					Tagged.Parent = touch.Parent.Humanoid
					Debounce2 = true
					wait(1)
					Debounce2 = false
					crit = false
					hitalready = true
					wait(2)
					hitalready = false
					punching = false
				end
				if hitalready == false and punching == true and touch.Parent ~= character then
					touch.Parent.Humanoid.Health = touch.Parent.Humanoid.Health - 10
					print("normal1")
					local Tagged = Instance.new("ObjectValue")
					Tagged.Name = "creator"
					Tagged.Value = player
					Tagged.Parent = touch.Parent.Humanoid
					Debounce2 = true
					wait(1)
					Debounce2 = false
					punching = false
				end
			end
		end)
	end
	
	local character = player.Character
	character.RightHand.Touched:Connect(function(touch)
		if Debounce2 == false and touch.Parent:FindFirstChild("Humanoid") and hitalready == false and punching == true  and touch.Parent ~= character and touch.Parent.Name ~= character.Name then
			if crit == true and hitalready == false and punching == true and touch.Parent ~= character  then
				touch.Parent.Humanoid.Health = touch.Parent.Humanoid.Health - 20
				print("crit2")
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = touch.Parent.Humanoid
				Debounce2 = true
				wait(1)
				Debounce2 = false
				crit = false
				hitalready = true
				wait(2)
				hitalready = false
				punching = false
			end
			if hitalready == false and punching == true and touch.Parent ~= character then
				touch.Parent.Humanoid.Health = touch.Parent.Humanoid.Health - 10
				print("normal2")
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = touch.Parent.Humanoid
				Debounce2 = true
				wait(1)
				Debounce2 = false
				punching = false
			end
		end
	end)
end)

Update:
It seems like this (the random damage instead of the normal 20 or 10 glitch) is a random glitch, im not sure if something triggers this but i think its random, we have tried everything… spamming space and lmb or spamming lmb only but it only works sometimes, the damage self thing is happening only to my friend? I still dont know why this happening…

Thanks in advance!!

1 Like
local debounce = false
local debounce2 = false
local combo = 1
local crit = false
local hitalready = false
local punching = false

--Create Animation--
local animation = script.Animation
local animation2 = script.Animation2

--Remotes--
local repStorage = game:GetService("ReplicatedStorage")
local critEvent = repStorage:WaitForChild("CritEvent")
local punchEvent = repStorage:WaitForChild("PunchEvent")

critEvent.OnServerEvent:Connect(function(player)
	crit = true
	wait(1)
	crit = false
end)

punchEvent.OnServerEvent:Connect(function(player, mouse)
	punching = true
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	if combo == 1 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation)
		animationTrack:Play()
		task.wait(1)
		combo = 2
		animationTrack:Stop()
		debounce = false
	elseif combo == 2 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation2)
		animationTrack:Play()
		wait(1)
		combo = 1
		animationTrack:Stop()
		debounce = false
	end

	character:WaitForChild("LeftHand").Touched:Connect(function(touch)
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)

	character:WaitForChild("RightHand").Touched:Connect(function(touch)
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)
end)
1 Like

It kind of worked?
I tried it out and it didnt work properly, it said that its an infinite yield on the humanoid part so i changed it from:

To:

local humanoid = character:WaitForChild("Humanoid")

It does the animation now but it does not damage? Ill try going through the code and thinking of a solution, but ill ask anyway… Thanks so much!!

Yeah, I meant to do character:WaitForChild("Humanoid") must’ve slipped by me while I was typing it all out (I didn’t do any debugging), try out the new edit in the above reply. Also please provide the local script which contains the FireServer() functions, thanks.

1 Like

its still not working, its still not damaging it but here is the local script:

local Player = game.Players.LocalPlayer -- get the localPlayer
local mouse = Player:GetMouse() --mouse

mouse.Button1Down:Connect(function() --mouse clicked
	game.ReplicatedStorage.PunchEvent:FireServer(mouse) --tell server
end)

seems to have the problem of:

even though i have this:
image

and the animations are here:
image

i dont know whats the cause of this?

Yeah, the damaging won’t work since it’s done after the animations are played but it seems the animations aren’t working from the original script, let me see.

local debounce = false
local debounce2 = false
local combo = 1
local crit = false
local hitalready = false
local punching = false

--Remotes--
local repStorage = game:GetService("ReplicatedStorage")
local critEvent = repStorage:WaitForChild("CritEvent")
local punchEvent = repStorage:WaitForChild("PunchEvent")

--Create Animation--
local animation = repStorage:WaitForChild("Animation")
local animation2 = repStorage:WaitForChild("Animation2")

critEvent.OnServerEvent:Connect(function(player)
	crit = true
	wait(1)
	crit = false
end)

punchEvent.OnServerEvent:Connect(function(player, mouse)
	punching = true
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	if combo == 1 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation)
		animationTrack:Play()
		task.wait(1)
		combo = 2
		animationTrack:Stop()
		debounce = false
	elseif combo == 2 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation2)
		animationTrack:Play()
		wait(1)
		combo = 1
		animationTrack:Stop()
		debounce = false
	end

	character:WaitForChild("LeftHand").Touched:Connect(function(touch)
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)

	character:WaitForChild("RightHand").Touched:Connect(function(touch)
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)
end)

Try this, I’ve moved the animations to repStorage.

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse() --mouse
local repStorage = game:GetService("ReplicatedStorage")
local punchEvent = repStorage:WaitForChild("PunchEvent")

mouse.Button1Down:Connect(function() --mouse clicked
	punchEvent:FireServer(mouse) --tell server
end)

Send the script which fires the CritEvent remote too please.

its pretty much that script (which sends a remote event) but its just sending it when i press space and lmb at the same time.
ill try to send it give me a second

update;
i printed a lot of stuff and found that when i print:

print(touch.Parent.Name)

in output it says “BagAccessory”
any ideas why?

also here is the printed script if you want to try it out for yourself:

local debounce = false
local debounce2 = false
local combo = 1
local crit = false
local hitalready = false
local punching = false

--Remotes--
local repStorage = game:GetService("ReplicatedStorage")
local critEvent = repStorage:WaitForChild("CritEvent")
local punchEvent = repStorage:WaitForChild("PunchEvent")

--Create Animation--
local animation = repStorage:WaitForChild("Animation")
local animation2 = repStorage:WaitForChild("Animation2")

print("A1")

critEvent.OnServerEvent:Connect(function(player)
	crit = true
	wait(1)
	crit = false
end)

print("A12")
punchEvent.OnServerEvent:Connect(function(player, mouse)
	print("A")
	punching = true
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	if combo == 1 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation)
		animationTrack:Play()
		task.wait(1)
		combo = 2
		animationTrack:Stop()
		debounce = false
	elseif combo == 2 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation2)
		animationTrack:Play()
		wait(1)
		combo = 1
		animationTrack:Stop()
		debounce = false
	end

	character:WaitForChild("LeftHand").Touched:Connect(function(touch)
		if touch.Parent.Name == character.Name then
			print("???")
		end
		print(character.LeftHand.Name .. " touched")
		print("seperate")
		print(hitalready)
		print(punching)
		print(touch.Parent.Name)
		print("seperate")
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			print("first if statement")
			if crit then
				print("second if statement")
				if debounce2 then
					print("debounce true")
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				print("hum -=20 not working")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					print("debounce2 true")
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				print("hum -=10 not working")
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)

	character:WaitForChild("RightHand").Touched:Connect(function(touch)
		print(character.RightHand.Name .. " touched")
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)
end)

Update2; it now prints “Player1” which is what its supposed to be. its still not printing that its going through the first if statement though?

Update3; i got information off this one.
i made my script like this:

local debounce = false
local debounce2 = false
local combo = 1
local crit = false
local hitalready = false
local punching = false

--Remotes--
local repStorage = game:GetService("ReplicatedStorage")
local critEvent = repStorage:WaitForChild("CritEvent")
local punchEvent = repStorage:WaitForChild("PunchEvent")

--Create Animation--
local animation = repStorage:WaitForChild("Animation")
local animation2 = repStorage:WaitForChild("Animation2")

print("A1")

critEvent.OnServerEvent:Connect(function(player)
	crit = true
	wait(1)
	crit = false
end)

print("A12")
punchEvent.OnServerEvent:Connect(function(player, mouse)
	print("A")
	punching = true
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	if combo == 1 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation)
		animationTrack:Play()
		task.wait(1)
		combo = 2
		animationTrack:Stop()
		debounce = false
	elseif combo == 2 then
		if debounce then
			return
		end
		debounce = true
		local animationTrack = animator:LoadAnimation(animation2)
		animationTrack:Play()
		wait(1)
		combo = 1
		animationTrack:Stop()
		debounce = false
	end

	character:WaitForChild("LeftHand").Touched:Connect(function(touch)
		if touch.Parent.Name == character.Name then
			print("???")
		end
		print(character.LeftHand.Name .. " touched")
		print("seperate")
		print(hitalready)
		print(punching)
		print(touch.Parent.Name)
		print("seperate")
		if touch.Parent:FindFirstChild("HumanoidRootPart") then
			print("1")
			if not hitalready then
				print("2")
				if punching then
					print("3")
					if not touch.Parent == character then
						print("first if statement")
						if crit then
							print("second if statement")
							if debounce2 then
								print("debounce true")
								return
							end
							debounce2 = true
							local hum = touch.Parent:WaitForChild("Humanoid")
							print("hum -=20 not working")
							hum.Health -= 20
							local Tagged = Instance.new("ObjectValue")
							Tagged.Name = "creator"
							Tagged.Value = player
							Tagged.Parent = hum
							task.wait(1)
							debounce2 = false
							crit = false
							hitalready = true
							task.wait(1)
							hitalready = false
							punching = false
						elseif not crit then
							if debounce2 then
								print("debounce2 true")
								return
							end
							debounce2 = true
							local hum = touch.Parent:WaitForChild("Humanoid")
							hum.Health -= 10
							print("hum -=10 not working")
							local Tagged = Instance.new("ObjectValue")
							Tagged.Name = "creator"
							Tagged.Value = player
							Tagged.Parent = hum
							hitalready = true
							task.wait(1)
							debounce2 = false
							punching = false
							hitalready = false	
			end
			end
			end
			end
		end
	end)

	character:WaitForChild("RightHand").Touched:Connect(function(touch)
		print(character.RightHand.Name .. " touched")
		if touch.Parent:FindFirstChild("HumanoidRootPart") and not hitalready and punching and not touch.Parent == character and not touch.Parent.Name == character.Name then
			if crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 20
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				task.wait(1)
				debounce2 = false
				crit = false
				hitalready = true
				task.wait(1)
				hitalready = false
				punching = false
			elseif not crit then
				if debounce2 then
					return
				end
				debounce2 = true
				local hum = touch.Parent:WaitForChild("Humanoid")
				hum.Health -= 10
				local Tagged = Instance.new("ObjectValue")
				Tagged.Name = "creator"
				Tagged.Value = player
				Tagged.Parent = hum
				hitalready = true
				task.wait(1)
				debounce2 = false
				punching = false
				hitalready = false
			end
		end
	end)
end)

and it printed this:
image

which means that it did not get through:

if not touch.Parent == character then

i dont know the problem though, it prints player1 as intended, that’s why its so confusing.

crit event script (starter player scripts):

--Variables--
local key = Enum.KeyCode.Q
local verify1 = false
local verify2 = false
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local timesUp = true

--Function--
local function event(input, gameProccess)
	if gameProccess then return end
	if input.KeyCode == key then
		verify1 = true
		local time1 = tick()
		return time1
	end
end

local function event2(input, gameProccess)
	if gameProccess then return end
	Mouse.Button1Down:Connect(function()
		verify2 = true
		local time2 = tick()
	end)
end

--Event--
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProccess)
	local time3 = tick()
	local time1 = event(input, gameProccess)
	local time2 = event2(input, gameProccess)
	if verify1 and verify2 then
		timesUp = false
		for i = 1, 3, 1 do
			if i == 3 then
				timesUp = true
			end
			if timesUp == false and verify1 == true and verify2 == true then
				game.ReplicatedStorage.CritEvent:FireServer()
				verify1 = false
				verify2 = false
			end
		end
	end
end)
	
game:GetService("UserInputService").InputEnded:Connect(function()
end)

thanks!

Try the stuff posted above by the way.

1 Like

Which one? Like which one above?

--Variables--
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")
local critEvent = repStorage:WaitForChild("CritEvent")
local verify1 = false
local verify2 = false
local timesUp = true

--Function--
local function event(input, gameProccess)
	if gameProccess then return end
	if input.KeyCode == Enum.KeyCode.Q then
		verify1 = true
		local time1 = tick()
		return time1
	end
end

local function event2(input, gameProccess)
	if gameProccess then return end
	mouse.Button1Down:Connect(function()
		verify2 = true
		local time2 = tick()
		return time2
	end)
end

--Event--
UIS.InputBegan:Connect(function(input, gameProccess)
	local time3 = tick()
	local time1 = event(input, gameProccess)
	local time2 = event2(input, gameProccess)
	if verify1 and verify2 then
		timesUp = false
		for i = 1, 3, 1 do
			if i == 3 then
				timesUp = true
			end
			if not timesUp and verify1 and verify2 then
				critEvent:FireServer()
				verify1 = false
				verify2 = false
			end
		end
	end
end)

UIS.InputEnded:Connect(function()
end)
1 Like

The most recent 3, which are all the latest changes to the 3 scripts you’ve provided.

1 Like

Oh okay i will, but got any idea why this

Is doing this (not going through)?

Oh, I removed most of the print commands. Feel free to add them back in.

1 Like

"if not touch.Parent == character then"

This makes sure that the part touching one of the arms of the punching player doesn’t belong to the punching player itself otherwise the player would damage itself.

1 Like

yes, but it still doesn’t go through even though the hand is not touching the character who attacked?

Are you testing this in-game or in Studio?

1 Like