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

Script keeps running even if the condition isn't met anymore. How do i fix it?

Asked by 4 years ago

To make everything clear, i don't want it to run just once. I want it to run non-stop while the player is touching the part and stop when he's not.

function onTouch(part)
        local check = part.Parent:FindFirstChild("Humanoid")
    while check ~= nil do
        wait(1)
        check.Parent.Humanoid.Health = check.Parent.Humanoid.Health - 1
    end
end
script.Parent.Touched:connect(onTouch)
0
use a if statement nickatnyte_yutub -5 — 4y

2 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

I'm keeping tract of who's touching the part and running through them to damage.

local part=--part
local touching={}

local running=false
function has()
    if running==false then running=true
        while #touching>0 do
            for _,v in pairs(touching) do
                if v:FindFirstChild("Humanoid") then
                    v.Humanoid:TakeDamage(10)
                end
            end
            wait()
        end
        running=false
    end
end
part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local good=true
        for _,v in pairs(touching) do
            if v==hit.Parent then
                good=false
            end
        end
        if good then
            table.insert(touching,hit.Parent)
            has()
        end
    end
end)
part.TouchEnded:Connect(function(hit)
    for i,v in pairs(touching) do
        if v==hit.Parent then
            table.remove(touching,i)
        end
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago
if script.Parent.Touched then
    function ()
        --ur code here
    end
end
0
i'm afraid it's only gonna run once (i've kinda already tried something similar) but i'll give it a shot anyway YoBoi1337 25 — 4y
0
ok try it. if it works thats really good, if not u can check the wiki for if statement nickatnyte_yutub -5 — 4y
0
doesn't run at all, no errors in the output either YoBoi1337 25 — 4y
0
also, i've already tried doing it by using the if statement, it only runs of code once and i don't want that YoBoi1337 25 — 4y
View all comments (5 more)
0
wait are you trying to make the person loose health or gain it fighterkirbyzx 102 — 4y
0
lose YoBoi1337 25 — 4y
0
try replacing line 5 with check.Parent.Humanoid:TakeDamage (1) fighterkirbyzx 102 — 4y
0
Nope, same thing. I still lose hp when i'm outside of it YoBoi1337 25 — 4y
0
sorry i forgot to add a while loop in the above script. add while true do [above whole code] end nickatnyte_yutub -5 — 4y

Answer this question