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

Why does this global value not change an IntValue?

Asked by 6 years ago

So i have 2 pieces of code in my hands:


--[]-- game.Players.PlayerAdded:connect(function(player) local leaderstats = Instance.new("Model") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Stage = Instance.new("IntValue") --We create a new IntValue Stage.Name = "Stage No." --this is the name you want the leader-stat to be when it shows up in-game. Stage.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later) Stage.Parent = leaderstats -- Set the money object as a child of the leaderstats object. end) --[]-- while true do game.Workspace.GameValues.StageNo.Value = _G.StageNo wait(0.1) end --[]-- Assets = {755156652,327081386,833322858} for _, asset in ipairs(Assets) do game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset) end --[]--

and:


_G.StageNo = 0 while true do script.Parent.Text = _G.StageNo print(_G.StageNo) wait(0.1) end
--[[My tree:
    game
        Workspace
            GameValues
                StageNo (an IntValue)
        ServerScriptService
            Code1
        StarterGui
            ScreenGui
                Frame
                    TextBox
                        Code2
]]

Problem: In Code 1, right after the function, I place a while true do statement wherein I try and assign a Global variable to the IntValue, but when the Global variable changes, the leaderstats of StageNo do not. Why?

0
Is this a server and local script? If so, "Keep in mind that the server has a different _G than clients do, so you can't define a global function in a server script and then use it from a LocalScript." mattscy 3725 — 6y
0
No no, they're both scripts. Normal scripts. RealRexTerm 21 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

The issue may of been your loops. Because when you have a loop that is outside of a event it yields the rest of the thread until it stops. So wrapping the loop in the thread with a coroutine.wrap() function would fix it and allow the rest of the code to run.

coroutine.wrap(function()
while true do
    game.Workspace.GameValues.StageNo.Value = _G.StageNo
    wait(0.1)
end
end)() -- For block 1
coroutine.wrap(function()
while true do 
    script.Parent.Text = _G.StageNo
    print(_G.StageNo)
    wait(0.1)
end
end)()
0
let me just google what a corountine wrap is. RealRexTerm 21 — 6y
0
Wait, i just realized that i have nothing assigning Stage Value to Int Value RealRexTerm 21 — 6y
0
I fixed it. Thanks for the script! RealRexTerm 21 — 6y
Ad

Answer this question