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

I am having trouble figuring out how to make two variables change on a timer, can anyone help me?

Asked by 6 years ago
Edited by shayner32 6 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I am having trouble debugging this script, I want to make it so that every second the Experience changes by 1 and so that every five seconds that the Points change by 1, but the problem is that only the experience changes, not the Points. Here is the code.

game.Players.PlayerAdded:connect(function(plr)
    local Stats = Instance.new("IntValue",plr)
    Stats.Name = "leaderstats"
     Experience = Instance.new("IntValue", Stats)
    Experience.Name = "EXP"
    Experience.Value = 0
     local Level = Instance.new("IntValue", Stats)
    Level.Name = "Level"
    Level.Value = 0
         local Points = Instance.new("IntValue", Stats)
    Points.Name = "Money"
    Points.Value = 0

        while true do
            Experience.Value = Experience.Value + 1
            wait(1)
        end

        while true do
            Points.Value = Points.Value + 1
            wait(5) 
        end




    Experience.Changed:connect(function()
        Level.Value = math.floor (Experience.Value / 1000)

    end)
end)

1 answer

Log in to vote
0
Answered by
shayner32 478 Trusted Moderation Voter
6 years ago
Edited 6 years ago

When you make a script, the script is given it's own "thread" (not really) to run on. So if you do a wait() in any part of the script, the entire script will be paused until the wait is over. You need to specify to Lua that you want to make it so that something can run on it's own. You can do this two ways:

with spawn() - http://wiki.roblox.com/index.php?title=Global_namespace/Roblox_namespace#spawn

or coroutines - http://wiki.roblox.com/index.php?title=Global_namespace/Coroutine_manipulation

You can find more and see examples on the ROBLOX wiki. Good luck!

Ad

Answer this question