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

how to make player damage every time he is inside of part?

Asked by 3 years ago

I used this script to make the player damage.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(5)
        wait(1)
    end
end)

But I don't know how to make the player take damage every 1 second he is inside of the part.

0
use a while loop with a if inside cjkizzy286 40 — 3y
0
wait no if cjkizzy286 40 — 3y
0
a while with the touched event cjkizzy286 40 — 3y

2 answers

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Make a script inside of the part:

local damage_delay = 1
local damage = 5

local part = script.Parent
local region = Region3.new(part.Position - (0.5 * part.Size), part.Position + (0.5 * part.Size))


while wait(damage_delay)
do
    for _, part in pairs(workspace:FindPartsInRegion3(region, nil, math.huge))
    do
        if part.Name == "HumanoidRootPart" then
        local human = part.Parent:FindFirstChild"Humanoid"
        if human ~= nil then
            human.Health = human.Health - damage
            end
        end
    end
end

What this does is creates a region that has the same size as the part and every time there is part inside of it, it checks if part is called humanoid root part and then it damages the player, damage delay is how fast it damages and damage is damage. Hope this helps.

Also you CAN use while loop but i do not recommend it as it may create a mess and damage you very very fast because you can touch the part again while inside of it and that will start the loop again.

Ad
Log in to vote
0
Answered by 3 years ago

so what you would have to do is use a while loop like this

script.Parent.Touched:Connect(function(hit)
    while true do
        hit.Parent.Humanoid:TakeDamage(5)
        wait(1)
    end
end)
0
Use a while loop in the middle of a touched event isn't needed. Dovydas1118 1495 — 3y

Answer this question