Combat system damage not working properly

  1. I basically made a system where you can kick and it will damage your opponent.
  2. since I am a beginner I stumbled into a problem that is allied with the way I scripted the combat system. the problem is that after I kick the damage OnTouch still goes on.

here you can see the problem
https://gyazo.com/8042f85d2d58e83b1292733174c522bf

here is the damage script.

local remote = game.ReplicatedStorage.Kick


function sussify(player)
	print(player)
	local canDamage = Instance.new("BoolValue")
	canDamage.Name = "canDamage"
	canDamage.Parent = player
	print(player)
	canDamage.Value = false
end

-- firing all the player exist [first player]
table.foreach(game.Players:GetPlayers(), function(i, p) sussify(p) end)

game.Players.PlayerAdded:Connect(sussify)

remote.OnServerEvent:Connect(function(plr, player)
	player.canDamage.Value  = true
	if player.canDamage.Value  == true then
		player.canDamage.Value = false
		player.Character["Right Leg"].Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid then
				humanoid:TakeDamage(2)
				player.canDamage.Value = false
			else
				return
			end
		end)
	end
end)

Disconnect the event then

local event; event = player.Character["Right Leg"].Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:TakeDamage(2)
		player.canDamage.Value = false
		event:Disconnect()
	end
end)

Although keep in mind that the event will still be made even if you aren’t touching anyone, think it’s best you do raycasting to detect if you’re close enough to damage

1 Like

woah thank you so much!! I know about disconnecting but I guess I did not think of it that way since my brain is not sync with scripting yet :rofl: . Thanks again!

1 Like