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

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

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)

1 comment

0

what is the error?

2 answers

✓ Accepted answer

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

1 comment

0

~= nil and == false isn't necessary. :P

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.

5 comments

0

nooooo, sorry, i expressed wrong, i mean the player owner of the script, not me, but the player that the script is in

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.

0

ultrasonic, i tried, and dont worked, i put the script in the player legs and arms, i did it wrong?

0

the error : 00:10:41.085 - Workspace.darkzerobits.Left Arm.Injure Brick:7: attempt to perform arithmetic on upvalue 'damage' (a function value)

0

Don't put it in the arms, put it inside of the part that the player is supposed to touch

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