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

Still does damage after attack is over? [Solved]

So I have been working on a hand to hand combat script on roblox and I got all the animations done and binded now I am trying to add damage to the attacks. Well that works fine the only problem is it still does damage after the attack so like when the character is touched. Any help? Here is the full code, code in question, and all properties of the script.

Full Code:


--[[
	Controls:
	Front Kick: E
	Side Kick: R
	Back Kick: Q
	Roundhouse Kick: G
	Quick Block: B
	Counter Punch: C
	Flying Side Kick: Y
	Axe Kick: T
	Push/Shove: F
	Strait Punch: Left Mouse Click
	Bow: Left Control
--]]

wait(1);

local player = game.Players.LocalPlayer;

local mouse = player:GetMouse();

local UPS = game:GetService("UserInputService");

local humanoid = player.Character:WaitForChild("Humanoid");

local disabled = script.DisableScript.Value;

--[[player.Character.Shirt.ShirtTemplate = script.Shirt.Value; --Example value http://www.roblox.com/asset/?id=269728305
player.Character.Pants.PantsTemplate = script.Pants.Value; --Example value http://www.roblox.com/asset/?id=269679048

player.Character.ForceField:Destroy();]]-- Works just disabling atm.

local function onInputEnded(input, gameProssesd)
	if input.UserInputType == Enum.UserInputType.Keyboard then

		local  KeyPressed = input.KeyCode;

		if disabled == 1 then
			print("Rip...");
		else
      --Side Kick: R
  		if KeyPressed == Enum.KeyCode.R then
  			local SideKick = humanoid:LoadAnimation(script.Animations.SideKick);
  			SideKick:Play();

  		--Push: F
  		elseif KeyPressed == Enum.KeyCode.F then
  			local Push = humanoid:LoadAnimation(script.Animations.Push);
  			Push:Play();

			script.Parent["Left Arm"].Touched:connect(function(target)
				local isHuman = target.Parent:FindFirstChild("Humanoid");
				
				if isHuman then
					target.Parent.Humanoid.Health = target.Parent.Humanoid.Health - script.Attacks.Push.Min.Value + math.random()*(script.Attacks.Push.Max.Value - script.Attacks.Push.Min.Value);
				end			
			end);

		--Front Kick: E
		elseif KeyPressed == Enum.KeyCode.E then
			local FrontKick = humanoid:LoadAnimation(script.Animations.FrontKick);
			FrontKick:Play();
			
		--Back Kick: Q
		elseif KeyPressed == Enum.KeyCode.Q then
			local BackKick = humanoid:LoadAnimation(script.Animations.BackKick);
			BackKick:Play();
			
		--Block: B
		elseif KeyPressed == Enum.KeyCode.B then
			local Block = humanoid:LoadAnimation(script.Animations.Block);
			Block:Play();
			
		--Block & Punch: C
		elseif KeyPressed == Enum.KeyCode.C then
			local Counter = humanoid:LoadAnimation(script.Animations.Counter);
			Counter:Play();
			
		--Flying Side Kick: Y
		elseif KeyPressed == Enum.KeyCode.Y then
			local FlyingSideKick = humanoid:LoadAnimation(script.Animations.FlyingSideKick);
			FlyingSideKick:Play();
			
		--Round House Kick: G
		elseif KeyPressed == Enum.KeyCode.G then
			local RoundHouseKick = humanoid:LoadAnimation(script.Animations.RoundHouseKick);
			RoundHouseKick:Play();
		
		--Axe Kick: T
		elseif KeyPressed == Enum.KeyCode.T then 
			local AxeKick = humanoid:LoadAnimation(script.Animations.AxeKick);
			AxeKick:Play();
			
		--Bow: Left Control
		elseif KeyPressed == Enum.KeyCode.LeftControl then
			local Bow = humanoid:LoadAnimation(script.Animations.Bow);
			Bow:Play();
  		end

  		mouse.Button1Down:connect(function()
  			local Punch = humanoid:LoadAnimation(script.Animations.Punch);
  			Punch:Play();
  		end)

		end

	end
end


UPS.InputEnded:connect(onInputEnded);

Code in question:

--Push: F
  		elseif KeyPressed == Enum.KeyCode.F then
  			local Push = humanoid:LoadAnimation(script.Animations.Push);
  			Push:Play();

			script.Parent["Left Arm"].Touched:connect(function(target)
				local isHuman = target.Parent:FindFirstChild("Humanoid");
				
				if isHuman then
					target.Parent.Humanoid.Health = target.Parent.Humanoid.Health - script.Attacks.Push.Min.Value + math.random()*(script.Attacks.Push.Max.Value - script.Attacks.Push.Min.Value);
				end			
			end);

All Children can be found here!

1 answer

Thanks to ProgrammerA10 for reminding me to Disconnect! Fixed code:

conn = script.Parent["Left Arm"].Touched:connect(function(target)
				local isHuman = target.Parent:FindFirstChild("Humanoid");
				
				if isHuman then
					target.Parent.Humanoid.Health = target.Parent.Humanoid.Health - script.Attacks.Push.Min.Value + math.random()*(script.Attacks.Push.Max.Value - script.Attacks.Push.Min.Value);
				end
				
				conn:Disconnect();
			end);

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