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)
The points value wont increase, how do I make my two loops co-exist?
4 answers
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)
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!
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.
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?
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
@ikiled You just forgot one end. Heh, other than that it worked great! Thanks so much!