Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

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

Asked by 9 months ago

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)

0
you can touch humanoids?! O_O!!!! Fifkee 886 — 9mo
0
ok, next time try using User Input service instead of mouse.KeyDown theking48989987 1853 — 9mo

2 answers

Log in to vote
0
Answered by 9 months ago

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 4) after the humanoid has taken damage then make debounce = to false again

Hope this helps :)

0
Actually ignore the 2nd instruction. Create another if statement under your "Touched" function to check debounce == false. NoirPhoenix 146 — 9mo
0
use edit button at botom of ur answor Fifkee 886 — 9mo
Ad
Log in to vote
0
Answered by 9 months ago

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)

Answer this question