Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

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

Asked by 5 months 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 24 — 5mo
0
wait no if cjkizzy286 24 — 5mo
0
a while with the touched event cjkizzy286 24 — 5mo

2 answers

Log in to vote
0
Answered by
imKirda 1188 Moderation Voter
5 months ago
Edited 5 months 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 5 months 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 1097 — 5mo

Answer this question