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

The points value wont increase, how do I make my two loops co-exist?

Asked by
iFurzy 35
7 years ago
    function onPlayerEntered(newPlayer)

        local stats = Instance.new("IntValue")
        stats.Name = "leaderstats"

        local points = Instance.new("IntValue")
        points.Name = "Currency"
        points.Value = 0

        local Prison = Instance.new("IntValue")
        Prison.Name = "Prison"
        Prison.Value = 10

        stats.Parent = newPlayer 
        points.Parent = stats
        Prison.Parent = stats

        while true do
        wait(1) 
                if Prison.Value >= 1 then
                Prison.Value  = Prison.Value - 1
            end
        end

        while true do
        wait(2)
                points.Value = points.Value + 50
            end
        end

game.Players.PlayerAdded:connect(onPlayerEntered)

4 answers

Log in to vote
0
Answered by
ikiled 75 Donator
7 years ago
Edited 7 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
    function onPlayerEntered(newPlayer)

        local stats = Instance.new("IntValue")
        stats.Name = "leaderstats"

        local points = Instance.new("IntValue")
        points.Name = "Currency"
        points.Value = 0

        local Prison = Instance.new("IntValue")
        Prison.Name = "Prison"
        Prison.Value = 10

        stats.Parent = newPlayer 
        points.Parent = stats
        Prison.Parent = stats


x = false -- set a boolean so that it can detirmine if its the correct time to go
while wait(1) do
 if Prison.Value >= 1 then
                Prison.Value  = Prison.Value - 1
            end
if x then -- this will make it so every 1 second it wont go, but 2 seconds it will
x = false
points.Value = points.Value + 50
else
x = true
end
end
game.Players.PlayerAdded:connect(onPlayerEntered)

0
@ikiled You just forgot one end. Heh, other than that it worked great! Thanks so much! iFurzy 35 — 7y
Ad
Log in to vote
1
Answered by
lukeb50 631 Moderation Voter
7 years ago
Edited 7 years ago

To run 2 loops at the same time you need to use a feature called Coroutines

Here is some documentation to help you:

Guide: http://wiki.roblox.com/index.php?title=Beginners_Guide_to_Coroutines

Function Dump: http://wiki.roblox.com/index.php?title=Function_dump/Coroutine_manipulation

You could also use Threading to run 2 loops at once

Tutorial: http://wiki.roblox.com/index.php?title=Threading

Hope This Helped!

Log in to vote
0
Answered by 7 years ago

I would not recommend doing that. putting anything below a while true do loop will not work unless it's in another function. and it wont repeat due to it only working once as it is in a player added function.

Log in to vote
0
Answered by
iOwn_You 543 Moderation Voter
7 years ago

you cant have 2 loops running at the same time on 1 script, what the computer does is read the script, can you read 2 books at the exact same time?

Answer this question