#Updating visual info on table change

1 messages · Page 1 of 1 (latest)

coral patrol
#

I have this script for an inventory system, In the script I have a table that holds the values of equipped towers, the problem is that the inventory icons arnt receiving the changed data until i close and reopen the inventory entirely.

hollow parrot
coral patrol
#

thats what iv already tried

#

give me a sec ill send the script

torn pivot
coral patrol
#
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local CoreEvent = ReplicatedStorage.RemoteEvents.CoreEvent
local InventoryGui = Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("Inventory")
local player = Players.LocalPlayer

local function ToggleInventory(input, processed)
    local uiClick = script.UIClick
    uiClick:Play()
    for i, v in pairs(InventoryGui.Icons:GetChildren()) do
        if v:IsA("ImageButton") and v.Name ~= "IconTemplate" then
            v:Destroy()
        end
    end
    local profileData = ReplicatedStorage.RemoteFunctions.GetProfile:InvokeServer(player)

    for i, tower in pairs(profileData.Inventory) do
        local towerModel = ReplicatedStorage.Towers:FindFirstChild(tower)
        local towerIcon = InventoryGui.Icons.IconTemplate:Clone()
        towerIcon.Name = tower
        towerIcon.Image = towerModel.Config.Hotbar.Image.Texture
        towerIcon.Parent = InventoryGui.Icons
        towerIcon.Visible = true

        towerIcon.Activated:Connect(function()
            print("CLICKED")
            -- Clear previous tower info
            InventoryGui.Info.Visible = false
            InventoryGui.Info.TowerName.Text = ""
            InventoryGui.Info.EquipButton.Visible = false
            
            -- Create tower info
            InventoryGui.Info.Visible = true
            InventoryGui.Info.TowerName.Text = towerModel.Name
            InventoryGui.Info.EquipButton.Visible = true
            if table.find(profileData.Equipped_Tower, towerModel.Name) then
                print("InTable")
                InventoryGui.Info.EquipButton.Text = "Unequip"
            elseif not table.find(profileData.Equipped_Tower, towerModel.Name) then
                print("NotInTable")
                InventoryGui.Info.EquipButton.Text = "Equip"
            end
        end)
    end
    if InventoryGui.Visible == false then
        InventoryGui.Visible = true
    else
        InventoryGui.Visible = false
    end
end

UserInputService.InputBegan:Connect(function(input, processed)
    if input.KeyCode == Enum.KeyCode.E then
        if not processed then
            ToggleInventory()
        end
    end
end)

InventoryGui.Parent.InventoryOpen.Activated:Connect(function()
    ToggleInventory()
end)

InventoryGui.Info.EquipButton.Activated:Connect(function()
    CoreEvent:FireServer("Tower", InventoryGui.Info.TowerName.Text)
end)
#

this is loca

#

local

coral patrol
dreamy moth
#

inventory is often the first

coral patrol
dreamy moth
#

whenever complex data changes, you have to update the ui as well if it is currently open

coral patrol
#

and how do i update the ui?

dreamy moth
#

depends on what it is and how you have it set up

#

bunch of different ways to listen for changes

coral patrol
dreamy moth
#

ya you wrote it so you should know it better than anyone else

coral patrol
#

exactly

#

just because i know it well doesnt mean i know how classic works

dreamy moth
coral patrol
#

okay, and what would you suggest is a way to go around it, iv already established that the ui isnt updating, i just dont know how to make it update

dreamy moth
#

local profileData = ReplicatedStorage.RemoteFunctions.GetProfile:InvokeServer(player) newb trap hehe you probably can't get live updates from this since you only fetch the data once when it opens, and this is currently your only way to discover changes

coral patrol
dreamy moth
#

doing it properly involves streaming the changes, so the server can inform the client of changes as they happen and the client reacts accordingly

#

rather than the client actively requesting the entire chunk of data at one particular moment when its needed

dreamy moth
coral patrol
#

im terrible at everything to do with player data

dreamy moth
#

profilestore is where your data is coming from; the inventory table

coral patrol
#

literally my weakest point

dreamy moth
#

now you have to do something else to get that inventory table to the client in a way that the client can listen for changes to it

#

there's so many ways you could do this

#

one rather naive way is to turn all the data in your inventory table into instances like have a folder for each item and a string/number value for each field, or you can use attributes in some way. and this is just one way and it is particularly beginner friendly. more advanced way is to use remote events to send the whole table once then small changes like 'change inventory slot x to y item with z count' 'empty slot b' aka the "delta" (change-of) etc

coral patrol
dreamy moth
#

you're not yet skilled enough to do it the advanced way

coral patrol
coral patrol
dreamy moth
coral patrol
#

Ill see what i can do

#

Ill be back soon

dreamy moth
coral patrol
#

I did it, made the event fire back from server after updating the table, it only sends back the changes and updates them accordingly, also added a ui update feature to reset the ui to the new data after the equip button is activated