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

How would I go around making a auto damaging part?

Asked by 10 years ago

For example: I would like to take 5 damage from the Character's Humanoid every 5 seconds if the player is still in 'contact' with the part. How would I go around creating this?

1 answer

Log in to vote
3
Answered by
User#2 0
10 years ago

You can utilize two methods here; the Touched event and the TouchEnded event. When the player touches the part, add them to a list. When they leave the part, take them off the list.

Touching = {}

function StartDamageLoop(PlayerName)
    repeat
        Touching[PlayerName]:WaitForChild("Humanoid"):TakeDamage(5)
        wait(5)
    until not Touching[PlayerName]
end

script.Parent.Touched:connect(function(Hit)
    -- Do your checks for the humanoid and make sure what hit the part is a player's character
    Touching[Hit.Parent.Name] = Hit.Parent
    StartDamageLoop(Hit.Parent.Name)
end)

script.Parent.TouchEnded:connect(function(Hit)
    if Touching[Hit.Parent.Name] ~= nil then
        Touching[Hit.Parent.Name] = nil
    end
end)

Note: This code is untested and may have bugs or not work, just rough code.

Ad

Answer this question