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

text on the sign wont change?

Asked by 5 years ago

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.

0
There should be a parent of the script and the intermission in the parent of the script. Maybe it should be script.Parent.intermission.SurfaceGui.Fram.TextLabel, or however many parents you have. iWagen 5 — 5y
0
maybe use: local text = script.intermission:FindFirstChild("SurfaceGui.Frame.TextLabel.Text") Nooberton_1 9 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

Reference vs Value

It is important that you know the difference between a reference and a value.

Reference

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.

Value

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.


Hopefully this answered your question and if it did then don't forget to hit that accept button. If you have any other questions then feel free to leave them in the comments below.
Ad

Answer this question