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

Nesting While loops?

Asked by
haillin 60
10 years ago

Greetings fellow Robloxians... I've been writing this script that does as follows:

(Player stands on brick the brick changes color if the players health isn't 100 it heals him and once he leaves the pad it changes back to the original color... )

(THE PROBLEM- The problem I'm having is that on the marked line bellow I can't figure out how to have the player heal only while he is standing on the pad.. If he moves he no longer receives healing. I tried to nest the while loop but had no luck. I've did some researching and articles say you nest while loops by:

while(condition) do while(condition) do statement(s) end statement(s) end

However I had no luck with that so I tried to use the 'and' identifier and once more I had no luck. I'd appreciate if a trained eye can view my code and tell me what I'm doing wrong, thank you very much in advance.

db=false
function heal(player)

if db then return end
db=true


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

if h~= nil and h:FindFirstChild("Value")== nil then

local brick=script.Parent   

character=h.Parent

torso=h.Parent.Torso

tp=torso.Position 

bp=brick.Position

if h.Health <100 then

print("Charging Heal-Pad!")

for i=1,10 do


wait(.1)    

brick.Transparency=brick.Transparency + .1





end

brick.BrickColor=BrickColor.new("Really red")
wait(.1)

for j=1,10 do

wait(.1)    

brick.Transparency=brick.Transparency - .1

end



while h.Health < 100 and (tp-bp).Magnitude<=5.6 do   ----------------------this line------------------------


wait(.111111)
h.Health=h.Health+4


end

print("All Healed! Step off to re")

x=Instance.new("StringValue")
x.Parent=h

while true do



wait(.5)

if torso:FindFirstChild("Value") ~= nil then break end

if (tp-bp).Magnitude>=5.6 then 


for _=1,10 do


wait(.1)    

brick.Transparency=brick.Transparency + .1


end

brick.BrickColor=BrickColor.new("Black")

for s_=1,10 do


wait(.1)    

brick.Transparency=brick.Transparency - .1  

NOOBMONSTER=Instance.new("StringValue") 
NOOBMONSTER.Parent=torso

end

end

end


 es=torso:GetChildren() 

for ess=1,#es do

if es[ess].Name==("Value") then es[ess]:Destroy() end

end

x.Parent=nil
db=false return end




end




db=false




end 


script.Parent.Touched:connect(heal)



2 answers

Log in to vote
0
Answered by 10 years ago

As with the other reply, I am at school so I can't test but I hope it works:

local heal = 4
local brick = script.Parent

brick.Touched:connect(function(obj)
    local on = false

    for i = 1, 10 do
        wait(.1)
        brick.Transparency = i/10
    end

    brick.BrickColor = BrickColor.new("Really red") wait(.1)

    for i = 9, 0, -1 do
        wait(.1)
        brick.Transparency = i/10
    end

    on = true
    brick.TouchEnded:connect(function(object)
        if obj == object then
            on = false
        end
    end)

    local human = obj.Parent:FindFirstChild("Humanoid")
    if human == nil then on = false end

    while on == true do
        if human.Health + heal > human.MaxHealth then
            human.Health = human.MaxHealth
            on = false
        end
        human.Health = human.Health + heal
        wait(.1)
    end
end)
Ad
Log in to vote
-1
Answered by 10 years ago

Hey I am not 100% sure as I made this in school and I am not able to test it out on studio so if this works I am glad that it helped if not then mind telling me the problem?

--[[
    This is not yet tested just made off a guess so if it works your welcome if not tell me what the error is
]]

local part -- Make sure to say what is this part
local on = false

part.Touched:connect(function(obj)
    if(obj ~= nil)then
        if (obj:FindFirstChild("Humanoid") ~= nil) then
            on = true
            CheckIsOn(obj, 1)
        end
        repeat wait() until obj == nil
        on = false
    end
end)

function ChangeHealth(person, amountHeal)
    while wait() do
        if on == true then
            if person:FindFirstChild("Humanoid").Health < 100 then
                person:FindFirstChild("Humanoid").Health = person:FindFirstChild("Humanoid").Health + amountHeal
            else
                break
            end
        else
            break
        end
    end
end

Answer this question