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?
✓ ANSWER ACCEPTED
How would I go around making a auto damaging part?
1 answer
✓ Accepted answer
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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
