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

different blocks of code in one script wont run?

Asked by 5 years ago
local debounce = false
--if dead is true
script.Parent.Touched:Connect(function(hit)
    print("touched")
    if script.Parent.Dead.Value == true and not debounce then
        debounce = true
        print("dead now")
        script.Parent.Parent:Destroy()
    end
end)
--


while true do
    wait()
    --gradually decrease oxygen
    delay(0.2,function()
        if script.Parent.Oxygen.Value <= 100 then
        script.Parent.Oxygen.Value = script.Parent.Oxygen.Value - 1
        end
    end)
    --



    --change color if oxygen is getting low, kill if its 0
    if script.Parent.Oxygen.Value < 50 and script.Parent.Oxygen.Value > 1 then
        script.Parent.BrickColor = BrickColor.new("New Yeller")
        end
    if script.Parent.Oxygen.Value >= 51 then
        script.Parent.BrickColor = BrickColor.new("Lime green")
        end
    if script.Parent.Oxygen.Value <= 0 then
        script.Parent.Dead.Value = true
    end
    --



    --turn death on if hp is 0
    if script.Parent.Health.Value <= 0 then
        script.Parent.Dead.Value = true
        script.Parent.BrickColor = BrickColor.new("Really black")
    end
    --





end

I have different things put together in one script because I want to keep everything neat and tidy. but some things like delay() wont work, another example is if I move the touched function down to the bottom it wont work either.

1 answer

Log in to vote
1
Answered by
megukoo 877 Moderation Voter
5 years ago

If you don't want an infinite loop to stop any code below it from running, the first step is to place it at the very bottom of your code.

local part = workspace.Part

part.Touched:Connect(function(other)
    print("who's there? " .. other.Name .. "'s there!")
end)

while true do
    --... code to repeat here. 
    wait(1)
end

If you want multiple while loops to run simultaneously, you can make use of the spawn() function. This will create a new thread the loop can run inside that won't interrupt the code outside the spawn().

local a = 1
local b = 2
local c = 3

while true do
    print(a .. ": value of loop 1")
    a = a + 1

    wait(1)
end

-- creates a new thread this loop can run upon
spawn(function()
    while true do
        print(b .. ": value of loop 2")
        b = b + 1

        wait(1)
    end
end)

-- multiple spawns won't interrupt others
spawn(function()
    while true do
        print(c .. ": value of loop 3")
        c = c + 2

        wait(1)
    end
end)

While spawn()s are great, try using them only when you need to.

Hope this helps!

Ad

Answer this question