Damage or Heal player when player touches something

0

Workspace/Part/Script
local brick = script.Parent

local function Touched(Part)
    if Part.Parent:FindFirstChild("Humanoid") then
        Part.Parent.Humanoid:TakeDamage(10) 
    end
end

brick.Touched:Connect(Touched)

Use a debounce to limit the touch to once per touch since a player has many parts in it's model and they will trigger many Touched events

Workspace/Part/Script
local brick = script.Parent

local touched = false -- debounce

local function ImTriggered(part)    
    local humanoid = part.Parent:FindFirstChild("Humanoid")

    if not touched and humanoid then
        touched = true
        print("Damage this human!")
        humanoid.Health = humanoid.Health - 10

        wait(1)
        touched = false
    end
end

brick.Touched:Connect(ImTriggered)

Healing works exactly the same way

Workspace/Part/Script
local brick = script.Parent

local touched = false -- debounce

local function ImTriggered(part)    
    local humanoid = part.Parent:FindFirstChild("Humanoid")

    if not touched and humanoid then
        touched = true
        print("Heal this human!")
        humanoid.Health = humanoid.Health + 10

        wait(1)
        touched = false
    end
end

brick.Touched:Connect(ImTriggered)

If you want the brick to disappear after touching it (for example "collecting" health coins) do this

Workspace/Part/Script
local brick = script.Parent

local touched = false -- debounce

local function ImTriggered(part)    
    local humanoid = part.Parent:FindFirstChild("Humanoid")

    if not touched and humanoid then
        touched = true
        humanoid.Health = humanoid.Health + 10

        brick:Destroy()

        wait(1)
        touched = false
    end
end

brick.Touched:Connect(ImTriggered)

  • 0 posts
  • 0 subtopics
  • 3 months ago by vince