Damaging Script! (You may use it if you want to)

Damaging Script!

What does this damaging script do? Well, whenever you touch the part you scripted it to it will hurt you.

Pic of the script:

5 Likes

cool script except we can’t really copy paste that bruh

1 Like

Script:

script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
if humanoid then
humanoid.Health = humanoid.Health-5
end
end)

1 Like

instead of

humanoid.Health = humanoid.Health-5

you can do

humanoid.health -= 5

to shorten the script.

4 Likes

this barely does damage not going to lie

Then change it if your really a programmer

Ummm

I’m not sure if this would be necessary for #help-and-feedback:cool-creations, it’s just a simple “Damage” script & that’s it

Plus there’s no debounce assigned to this Part, which would more than likely result in a lot of fired events happening

4 Likes

challenge accepted

local DAMAGE = 25
script.Parent.Touched:Connect(function(hit)
  wait(1)
  local humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
  if humanoid game.Players:GetPlayerFromCharacter(hit.Parent) then
   humanoid.Health -= DAMAGE
  end
end)

you can change the DAMAGE value to change the Damage per touch this also will only damage players

Good job lol I meant change the damage in his script

you can do that by changing the damage variable on top

Real programmers can figure out what it does and how to use it

Version with debounce, for more productive/reliable touch events:

local Debounce = false
local DebounceTime = .5 -- change this to the amount of time you need before it damages again
local Damage = 5 -- amount of damage done to the player

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
    if humanoid then
        if Debounce == false then
            Debounce = true
            humanoid.Health = humanoid.Health - Damage 
            wait(DebounceTime)
            Debounce = false
        end
    end
end)
1 Like

Instead of

local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

you can just do

local humanoid = hit.Parent:FindFirstChild("Humanoid")

Also as suggested previously, you should add a debounce.

That is an interesting use of “WhichIsA”. As @TeaCem pointed out, you may as well use FindFirstChild, as it does the same function. However, if the name of the “Humanoid” part itself is changed for whatever reason (say they have a CustomCharacter, and they decided to name the humanoid “hum”), then that would come in handy.

1 Like

Consider using :TakeDamage() instead since a force field would prevent the script from functioning properly.

This is cool! This will definitely help scripters just starting out. There is some things I would do to make this better and more effective. I would first shorten the line of code that damages the player by doing:

humanoid.Health -= 5

You could also use :TakeDamage() instead which would prevent the player from taking damage when they have a forcefield. I would also add a debounce system which would prevent the script from spamming the damage onto the player (unless that is what you want). Then, finally, I would add a variable for the damage so it isn’t left as what they call a “magic number.” All together it would look something like this:

local damage = 5 
local cooldown = 1
local debounce = false

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
    if humanoid and debounce == false then
        debounce = true
        humanoid.Health -= damage --//Or alternatively: humanoid:TakeDamage(damage)
        wait(cooldown)
        debounce = false
    end
end)

Or you can do this…

local debounce, part, cooldown, damage = false, script.Parent, 1, 5
part.Touched:Connect(function(hit)
	if debounce then return end
	local parent = hit.Parent
	if not parent then return end
	local humanoid = parent:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	debounce = true
	humanoid:TakeDamage(damage)
	wait(cooldown)
	debounce = false
end)