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)
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
end
s:connect
and:findFirstChild
are deprecated, use:Connect
and:FindFirstChild
instead- try understanding a bit more the
if
things
hope this helped
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.