code:
local teams = game:GetService("Teams"):GetTeams() local text = script.intermission.SurfaceGui.Frame.TextLabel.Text for _, team in pairs(teams) do local players = game.Teams.Playing:GetPlayers() print(#players) if #players == 0 then text = 'test' print('ok') end end
it prints ok but it will not change the text on the sign.
The problem here is that you incorrectly assume that the text
variable refers to the actual property of the TextLabel
. If the Text
changes, your variables will not change along with it. They'll stay the same. Take this for example:
-- Currently the IntValue holds a Value of 25. local int = workspace.IntValue.Value workspace.IntValue.Value = 50 -- Changing value. print(int) --> 25, it does not update along with it. int = 92 -- Here it's just reassigning 'int'. print(workspace.IntValue.Value) --> 50, it does not change even after reassigning 'int'.
To fix the problem you either directly access the property and change it or have your variable hold the text label object.
local teams = game:GetService("Teams"):GetTeams() local text = script.intermission.SurfaceGui.Frame.TextLabel for _, team in pairs(teams) do local players = game.Teams.Playing:GetPlayers() print(#players) if #players == 0 then text.Text = 'test' -- Accessing the property! print('ok') end end
It is important that you know the difference between a reference and a value.
In Lua, for example, functions are passed by reference.
local func1, func2 func1 = function() print(1) end func2 = function() print(1) end local func1reference = func1 -- func1 and func1reference are referring to the same function now, but func2 is a different one. print(func1reference == func1) --> true print(func1 == func2) --> false, they do not reference the same function print(function() end == function() end) -- false, they do not reference the same function
The func1
and func1reference
variables both hold the same exact function. func2
is a different function from func1
even though it contains the same exact code. Remember, references.
But data like booleans and strings are simply passed by value.
local _string = "hello" local _string_value = _string _string = "ok" print(_string_value) --> hello print(_string) --> ok local bool1 = false local bool1_val = bool1 bool1 = true print(bool1) --> true print(bool1_val) --> false
Even though _string_value
was set to _string
, both variables have different memory and therefore changing one value does not change the other. The same happened for the boolean values, bool1_value
never changed even after updating bool1
.