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

when i touch a humanoid it kills me! help??

so i am trying too make a punch script that does damage according to eaderstats, and instead of increasing it just kills me when i touch the root part of a humanoid NOTE: this is a localscript

player = game.Players.localPlayer
local Char
	repeat
	    Char = player.Character
	    wait()
until Char

local Humanoid = Char:WaitForChild("Humanoid")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local dmg = true

Mouse.KeyDown:connect(function(key)
 if key == "q" then 
  local an = script.Parent.Humanoid:LoadAnimation(script.Animation)
  an:Play()
  script.Parent.LeftHand.Touched:connect(function(hit)
  local Player = game.Players.LocalPlayer
	local LB = Player:WaitForChild("leaderstats")
	local p = LB:WaitForChild("Power")
	local damage = 0.1 * p.Value
	wait(1)
	 	Humanoid:TakeDamage(damage)

    dmg = false
    wait(1)
    dmg = true
   end)


  end
 
end)

2 comments

0

you can touch humanoids?! O_O!!!!

0

ok, next time try using User Input service instead of mouse.KeyDown

2 answers

Hi, the reason u keep dying instantly is because there is no debounce to fall on therefore your script is getting triggered multiple times.

To do this:

  1. add a variable (local debounce = false)
  2. add validation to your keybind detection (if key == "q" and debounce == false) then 3)after that validation turn the debounce to true
  3. after the humanoid has taken damage then make debounce = to false again

Hope this helps :)

2 comments

0

Actually ignore the 2nd instruction. Create another if statement under your "Touched" function to check debounce == false.

0

use edit button at botom of ur answor

The damage isn't encased in the "hit" function you referred to. Add something like..

function hit()
	local eHumanoid = script.Parent.LeftHand.Touched.Parent:FindFirstChild('Humanoid')
	-- ^ Finds out if the thing you're punching is a Humanoid.
	local damage = 0.1 * p.Value -- The Damage value.
	if (eHumanoid) then -- Checks if the Variable found a humanoid.
		eHumanoid:TakeDamage(damage) -- If it did, said humanoid takes damage.
	end
end

This may need some tweaking, but judging from your code its the best I could come up with.

EXTRA NOTES: Don't use connect, it's been updated to Connect (with a capital C)

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