Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my TextLabel not change?

Hey, so I put a local script with the code below inside as a child of a TextLabel. My idea was that if there are less than 5 players in the game, the text inside the TextLabel would change to say how many people are needed (run upon joining), and if there are more than 5 and the game is loading, you state how many seconds there are before the next round starts

contents = script.Parent.Text
players = game:GetService('Players')
plrArr = players:GetChildren()
rs = game:GetService('ReplicatedStorage')
status = rs:WaitForChild('Status')
events = rs:WaitForChild('Events')
tc = events:WaitForChild('TimerChange')

if status.Value == 'Waiting' then
	print('waiting')
	contents = 'WAITING FOR ' .. tostring(5 - #plrArr) .. ' MORE PLAYERS'
elseif status.Value == 'Loading' then 
	contents = 'GAME STARTING IN ' .. rs:WaitForChild('TimerSecs').Value .. ' SECONDS'
end

tc.OnClientEvent:Connect(function(secs) 
	if status == 'Loading' then
		contents = 'GAME STARTING IN ' .. secs .. ' SECONDS'
	end
end)

The print function runs, but the text stays as its default. Any ideas why? any help would be much appreciated. Thanks in advance

1 answer

✓ Accepted answer

Two words: Indirect assignment.

Assigning script.Parent.Text to a variable, and then proceeding to change the value of said variable will change the value of the variable but it will not change the value of the Text. This is because the value assigned to the variable is static, and therefore never changes. Not only this, but Text itself is still dynamic yet cannot be changed by assigning its original value to a variable, where any changes made to the variable will never replicate to the Text because the variable only originally referred to the original Text and is not directly linked to the property.

What's the solution, then? If you assign a variable to script.Parent and then do <var>.Text, that change is actually made to the GUI. Try doing this instead of directly referring to Text.

1 comment

0

Ah thank you so much! I just came back from a long period of using node.js so I completely forgot stuff like this. It's much appreciated

This discussion is preserved as it was. New answers, comments, and votes are closed.