Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My damage script dont work,this script will be in the arms and legs of a player,what i did wrong?

Asked by 6 years ago
Edited 6 years ago

i created a script that when you touch, if you isnt the owner(all players, but will hurt only other players, and not himself), you will get hurt,this script is for a combat system, i put this script in the arms and legs of each player, but dont work, what i did wrong?

local debounce = false 
local HealthLoss = 14
function OnTouched(Part) 
                if script.Parent.Parent.Name == Part.Parent.Name then
        else
        if Part.Parent ~= nil then--Keep Parentless blocks from breaking it. 
if debounce == false and Part.Parent:findFirstChild("Humanoid") ~= nil then 
debounce = true 
Part.Parent:findFirstChild("Humanoid"):TakeDamage(HealthLoss) 
wait(0.2)--Wait two seconds before the brick can hurt someone. 
debounce = false 
end 


end 
script.Parent.Touched:connect(OnTouched)

0
what is the error? TheGreatSailor 20 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

do this:

local debounce = false 
local HealthLoss = 14

function OnTouched(Part) 
    if script.Parent.Parent.Name ~= Part.Parent.Name then
        if Part.Parent ~= nil then 
            if debounce == false and Part.Parent:FindFirstChild("Humanoid") ~= nil then 
                debounce = true 
                Part.Parent.Humanoid:TakeDamage(HealthLoss) 
                wait(0.2) 
                debounce = false
            end
        end
    end
end
script.Parent.Touched:Connect(OnTouched)

here's some errors i'm pointing out:

  • you missed a few ends
  • :connect and :findFirstChild are deprecated, use :Connect and :FindFirstChild instead
  • try understanding a bit more the if things

hope this helped

0
`~= nil` and `== false` isn't necessary. :P TheeDeathCaster 2368 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

It would likely be more efficient to put the script inside of the actual part that will be touched instead of the player, which I will explain and write here:

local damage = 14 --change this number if you want them to be damaged more
debounce = false
function damage(h)
if h.Parent.Name ~= "darkzerobits" and debounce = false -- if the person who touches the block isnt equal to the owners name which i assume is you
and h.Parent:FindFirstChild("Humanoid") then --check if this is an actual player, sometimes the part can touch the floor or something and cause an error if this line isnt in
debounce = true
h.Parent.Humanoid.Health = h.Parent.Humanoid.Health - damage --damage the player by subtracting their current health from the damage number we set
wait(3)
debounce = false
end
end
script.Parent.Touched:connect(damage)

EDIT: You may have noticed that there is a "debounce" thing inside of the script. If you don't know, that's simply just to prevent the script from damaging the character too much, as sometimes one touch can translate to 5 touches, for example. Debounce basically only allows one touch out of those 5 touches to go through, meaning that the result will only occur one time with that one touch.

0
nooooo, sorry, i expressed wrong, i mean the player owner of the script, not me, but the player that the script is in darkzerobits 92 — 6y
0
Oh, then in that case you can simply just replace your name with script.Parent.Name and make the player's name the name of the part, similarly to what you tried to do. ultrasonicboomer 85 — 6y
0
ultrasonic, i tried, and dont worked, i put the script in the player legs and arms, i did it wrong? darkzerobits 92 — 6y
0
the error : 00:10:41.085 - Workspace.darkzerobits.Left Arm.Injure Brick:7: attempt to perform arithmetic on upvalue 'damage' (a function value) darkzerobits 92 — 6y
0
Don't put it in the arms, put it inside of the part that the player is supposed to touch ultrasonicboomer 85 — 6y

Answer this question