Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

1

Use coroutines. http://wiki.roblox.com/index.php?title=Coroutine  It's almost like a separate script running in one.

0

Coroutines are not the optimal solution here, although they will technically work.

3 answers

✓ Accepted answer

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)

2 comments

0

It gives me my levels too fast...

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.

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

while true do
wait()
break
end

9 comments

0

I do not believe that's what the Asker was asking for.

0

I was showing him that it is possible to have multiple loops because you can break out of a loop.

1

He wanted both while loops to run simultaneously...

0

My mistake. Sorry for trying to help. c;

0

And maybe if you know everything, why don't you try helping him? Or me for that matter? qq

0

No need to be an ass about it, Vexture.

0

He's the one being the ass. <3

2

gets some popcorn ready

0

lol, Perci. It's not like we're dropping f-bombs here. AFAIK, you can actually say 'ass' on ROBLOX now.

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)

5 comments

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.

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.

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.

0

I was joking, he criticized me for not tabbing my code earlier.

0

I'll also down vote. It's too hard to read.

This discussion is preserved as it was. New answers, comments, and votes are closed.