How can I update the text in this script?

Sorry for the long code here, but I’m making a shop at the moment or at least trying to the best I can, but I’m trying to figure out how I can update gold value in this script.

I have a module script and the default gold is 50, but when the player buys something I multiply it by 2 and update the number so it goes from 50 to 100 and it works as my gold does get subtracted correctly the issue is the gold text or the gold value in this script doesn’t change so in my gui it still says 50 and I can’t seem to figure out how to get it do show the updated gold number.

--[Services]--
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")

--[Variables]--
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local tavernInfo = require(game.ReplicatedStorage.Tavern)
local Purchase = RepStorage:WaitForChild("purchase")
local data = RepStorage:WaitForChild("data")

--[Shop]--
local Shop = PlayerGui:WaitForChild("Shop")
local tavernShop = Shop.Tavern
local tavernShopButtons = tavernShop:GetChildren()

local item
local rep
local desc
local gold
local totalAmount
local amountOwned

for i,v in pairs(tavernShopButtons) do
	if v.ClassName == "TextButton" then
		v.MouseButton1Click:Connect(function()
			if v.Name ~= "Buy" then
				if v.Name ~= "Purchase" then
					print(v.Name)
					item = tavernInfo[v.Name]
					rep = item.Rep
					desc = item.Desc
					gold = item.Gold
					totalAmount = item["Total Amount"]
					--amountOwned = game.ServerStorage:WaitForChild("playerData")[Player.Name]["Owned Items"]["Tavern Items"][item.Name].Value
					tavernShop.name.Text = v.Name--.." ("..amountOwned.."/"..totalAmount..")"
					tavernShop.rep.Text = "Reputation +"..rep
					tavernShop.desc.Text = desc
					local checkifOwned = data:InvokeServer("tavern",item)
					if checkifOwned == true then
						tavernShop.Buy.Text = "MAXXED"
					else
						tavernShop.Buy.Text = "BUY "..gold
					end
					print(item.Name.." selected")
				elseif v.Name == "Purchase" then
					--do nothing for now, but gonna make a gamepass for this
				end
			elseif v.Name == "Buy" then
				print("buying "..item.Name)
				local test = Purchase:InvokeServer("tavern",item)
				if test == true then
					print("successfully bought "..item.Name)
				elseif test == false then
				end
			end
		end)
	end
end

p.s: sorry for messy and not good code, but this is what I got so ye

Have you tried doing

local gui = -- whatever the gui is
 gui.Text = gold.Value

You can edit the property of a textLabel called “Text” to your gold.Value (Aka. How much gold you have)

1 Like

Do you have the gold amount value as an object (like a numValue/intValue) in the game? Or is it stored in a modulescript/script as a value?

An object is usually where you have it, atleast that’s what I do (Also intvalue)

1 Like

yeah it’s in a module script this is what I have

local Tavern = {
	
	["Stools"] = {
		["Name"] = "Stools";
		["Total Amount"] = 6;
		["Rep"] = 20;
		["Gold"] = 50;
		["Desc"] = ""
	};
	
	["Tables"] = {
		["Name"] = "Tables";
		["Total Amount"] = 6;
		["Rep"] = 20;
		["Gold"] = 50;
		["Desc"] = ""
	};
	
	}

function Tavern.priceUpdate(item,newgoldcost)
	Tavern[item.Name].Gold = newgoldcost
end

return Tavern

the function seems to work as I’ve tried doing print(Tavern[item.Name].Gold) and it does change from 50 to 100 to 200 etc :confused:

edit: btw I’m still a bit new to module scripts

Then in that case, the use of “.Changed” would be a good idea.

EDIT:
Example usage:

value = game.ReplicatedStorage:WaitForChild("NumValue")

value.Changed:Connect(function()
    if value.Value == 5 then
        print("The Value Is Now 5!")
    end
end)

It basically checks for whenever the value of the object changes, then does what’s below it.

1 Like

Oh you can either make a while loop for the gui to always equal the amount of gold in the table or making it equal the amount after they buy something, I don’t know if 100% if this will work since I don’t use module scripts or table in these ways as much

I recommend you don’t use an infinite loop, as it’s innefficient and generally not a good practice. As I mentioned earlier, the use of the “.Changed” event is a more suitable option. And it’s not hard to use whatsoever.

Here’s a link from Roblox on it: NumberValue | Documentation - Roblox Creator Hub

I didn’t know if you could use .Changed event for a table value(pretty sure it has to be an object value)
Edit: Wait lol, i think I’m getting confused here… should of went to sleep hours of go :eyes:

Ah true. There is another way you could go about this. You could have a function that you call on every time you change the table value. Something like this:

local function setGold(goldTable, newGold)
    newGold = goldTable.gold
    -- whatever you want to happen when the gold changes
end

Then just call on setGold() whenever you have something that changes it.

The idea is from here: .Changed() for tables? - #7 by SunstarVIII

EDIT: I realize that the way I mentioned is different from what he said. You can check out his idea in the link I sent.

Sorry if this is dumb to ask, but what would goldTable be? also where do I put this? lol

Oh I didn’t see this from earlier. That’s basically the same thing as I mentioned, whoops. So it’s working you said?

I assume the part he needs in that script is how to make the text for his gui, equal the newGold value

Since you already have that “Tavern.priceUpdate” function, you could spass that over to a localscript. Then in the localscript you can do something like:

gui.Text = Tavern[item.Name].Gold

EDIT: Or another way you could do it is to have an intValue or numValue placed in ServerStorage. Then use that “Taver.priceUpdate” function to update it every time the gold value changes. Then finally, in the localscript do something like the snippet above, with the value for the gui.Text to change to be the
.Value of the object in ServerStorage. Like:

local ServerStorage = game:GetService("ServerStorage")
local goldValue = ServerStorage:WaitForChild("Gold")

gui.Text = goldValue.Value
2 Likes

still didn’t fully get it to work properly, but I made progress so thanks for the help!

Okay, good to know I helped somewhat. Let me know if you have any other questions with this.

1 Like