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
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.
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.
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
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.
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