Textlabel not updating when changed in local script

Hi, I’m creating a system where you pick cards to gain better stats and this pops up as a GUI on your screen. The cards you get are randomised and I want the information to be given to the player when they open this. The problem is the cards text/text description doesn’t update. I have already looked into the topic and changed my scripts to local scripts + made sure it checks PlayerGui and not StarterGui, and it also prints the correct values when i debug in game, so why does the text not update for the player?

local player = game:GetService("Players").LocalPlayer
local button = player.PlayerGui.Cards.TurnON
local cardsparent = player.PlayerGui.Cards
local function leftClick()
for i, v in pairs(cardsparent.ActualCards:GetDescendants())do	
		if v:IsA("ImageButton") then
			v.Visible = true
			local chances = math.random(1,8)
			local cardname = v.CardTitle.Text
			local carddesc = v.CardDesc.Text
			local card = v.TheCard.Value

			if chances == 1 then
				card = "Health"
				cardname = "Health"
				carddesc = "+10 Health"
			end
	end
		end
button.MouseButton1Click:Connect(leftClick)

1 Like

You’re only changing the variable, not the actual properties of the instances.

cardname is just the string value of the .Text property, same with carddesc, and card is just the actual value.

As you can see, all you did here was change the variables.

The correct way to do it would be to store the instance themselves, not their property value, and then change that property value by indexing for it

			local chances = math.random(1,8)
			local cardname = v.CardTitle --we are storing a reference to these instances
			local carddesc = v.CardDesc
			local card = v.TheCard

			if chances == 1 then
				card.Text = "Health" --we are changing the properties
				cardname.Text = "Health"
				carddesc.Value = "+10 Health"
			end

This is just how Lua works. In order to change a table value, you have to index it in the statement.

local table1 = {table2 = {Text = 'abc'}}
local table2 = table1.table2 --store a reference
table2.Text = 'hi' --change a value inside table2
print(table1.table2.Text) --> "hi"
print(table2.Text) --> "hi"
local table1 = {table2 = {Text = 'abc'}}
local table2 = table1.table2.Text --store the value
table2 = 'hi' --change the variable itself
print(table1.table2.Text) --> "abc"
print(table2) --> "hi"

Thanks for the help, I didn’t know I made such a simple mistake :sweat_smile: thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.