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

How would I add 2 while loops in a single function?

Asked by 9 years ago

I have a script, It has 1 while loop so every __ seconds it will give mana. The number of seconds depend on the player's level. I want the player's level to go up every minute they play the game. Please help!

game:GetService("Players").PlayerAdded:connect(function(plyr)
    local stats = Instance.new("IntValue", plyr)
    stats.Name = "leaderstats"

    local lvl = Instance.new("IntValue", stats)
    lvl.Name = "Level"
    lvl.Value = 1

    local mana = Instance.new("IntValue", stats)
    mana.Name = "Mana"
    mana.Value = 0

    while wait(1/lvl.Value) do
        mana.Value = mana.Value+1
    end
end)
1
Use coroutines. http://wiki.roblox.com/index.php?title=Coroutine  It's almost like a separate script running in one. M39a9am3R 3210 — 9y
0
Coroutines are not the optimal solution here, although they *will* technically work. adark 5487 — 9y

3 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

By using delta time, you can synchronize the two events separately within the same loop:

game:GetService("Players").PlayerAdded:connect(function(plyr)
    local stats = Instance.new("IntValue", plyr)
    stats.Name = "leaderstats"

    local lvl = Instance.new("IntValue", stats)
    lvl.Name = "Level"
    lvl.Value = 1

    local mana = Instance.new("IntValue", stats)
    mana.Name = "Mana"
    mana.Value = 0

    local last = tick()
    local elapsed = 0
    local otherElapsed = 0
    while true do
        local t = tick()
        local dt = t - last
        last = t
        elapsed = elapsed + dt
        otherElapsed = otherElapsed + dt

        local manaUp = (1/lvl.Value)
        if otherElapsed > manaUp then
            while otherElapsed >= manaUp do -- It's possible for the minimum wait to be *more* than the time it takes to gain mana.
                mana.Value = mana.Value + 1
                otherElapsed = otherElapsed - manaUp
            end
        end

        if elapsed > 60 then --one minute
            lvl.Value = lvl.Value + 1
            elapsed = elapsed - 60
        end

        wait()
    end
end)
0
It gives me my levels too fast... EzraNehemiah_TF2 3552 — 9y
0
I solved the error. I wasn't overwriting the `last` variable I set on line 13, I was accidentally creating a whole new one. My code should now work. adark 5487 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

In order to break out of a loop, you would do something like this:

while true do
wait()
break
end
0
I do not believe that's what the Asker was asking for. M39a9am3R 3210 — 9y
0
I was showing him that it is possible to have multiple loops because you can break out of a loop. dirty_catheter 7 — 9y
1
He wanted both while loops to run simultaneously... M39a9am3R 3210 — 9y
0
My mistake. Sorry for trying to help. c; dirty_catheter 7 — 9y
View all comments (5 more)
0
And maybe if you know everything, why don't you try helping him? Or me for that matter? qq dirty_catheter 7 — 9y
0
No need to be an ass about it, Vexture. adark 5487 — 9y
0
He's the one being the ass. <3 dirty_catheter 7 — 9y
2
*gets some popcorn ready* Perci1 4988 — 9y
0
lol, Perci. It's not like we're dropping f-bombs here. AFAIK, you can actually say 'ass' on ROBLOX now. adark 5487 — 9y
Log in to vote
-2
Answered by 9 years ago
game:GetService("Players").PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue",player) --tabbing is unecessary
stats.Name = "leaderstats"
local lvl = Instance.new("IntValue",stats)
lvl.Name = "Level"
lvl.Value = 1
local mana = Instance.new("IntValue",stats)
mana.Name = "Mana"

coroutine.resume(coroutine.create(function()--coroutines! They let you run functions on a separate thread, meaning other code can run at the same time.
while wait(60) do
lvl.Value = lvl.Value + 1
end
end))

while wait(1/lvl.Value) do
mana.Value = mana.Value + 1
end
end)
0
Like Dark said, wait() has a minimum, so level won't really affect anything after you reach level 34. I believe can use renderstepped to double that. aquathorn321 858 — 9y
0
I downvoted this simply for your comment that tabbing is unnecessary. Tabbing is *very* necessary for writing legible code. The Lua interpreter ignores it, so it's not like it's any faster to not use it. adark 5487 — 9y
0
Also, it could potentially be a *good* thing to limit the mana gains to 30 per second maximum, since `wait()` waits 1/30th of a second minimum. adark 5487 — 9y
0
I was joking, he criticized me for not tabbing my code earlier. aquathorn321 858 — 9y
0
I'll also down vote. It's too hard to read. EzraNehemiah_TF2 3552 — 9y

Answer this question