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

(SOLVED) How to end while loop inside of Touched event?

Asked by
Lazarix9 245 Moderation Voter
4 years ago
Edited 4 years ago

I've got this script:

local part = script.Parent


part.Touched:Connect(function(h)

    local humanoid = h.Parent:FindFirstChild("Humanoid")

    while humanoid do

        humanoid:TakeDamage(0.3)
        wait(0.5)

    end

end)

When the part is touched, humanoid is supposed to take damage while character is touching the part, but when the touch ends, the damage taking is supposed to stop. That won't work. I've tried saying this:

part.TouchEnded:Connect(function(h)
end)

underneath that but it wouldn't work either.

I'd also like one more thing. When the part is being stood on the damage increases and I don't want that. This is not necessary but if you can do it I'd be very thankful.

Thanks ^^

1
or break Thepoint13 99 — 4y
0
May be simple, but I still dont understand ;-; Lazarix9 245 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

No loop Note: Most likely be like kill function

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 0-.3 -- Change the amount to change the damage.
wait(0.5)  -- seconds before activating IF still touching. 
        end
end
script.Parent.Touched:connect(onTouched)

Loop Note: Acts more like kill function

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 0-.3 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)
0
Nope, didn't work. Thank you for taking time to reply though. Lazarix9 245 — 4y
Ad
Log in to vote
0
Answered by
Lazarix9 245 Moderation Voter
4 years ago

Hi, so, I kinda solved my problem with this, I added this script:

local part = script.Parent
local touching

part.Touched:Connect(function(h)

    local humanoid  = h.Parent:FindFirstChild("Humanoid")
    touching = true

    while touching == true do

        humanoid:TakeDamage(0.3)
        wait(0.5)

    end 

end)

part.TouchEnded:Connect(function(h)

    touching = false

end)

so I just added a variable then assigned it to true in the touched event and false in the touchended event and inside of the while loop in the touched event i put while touching == true as a condition that's it sorry for wasting your time

Answer this question