Im making an inventory system and the status is either Owned or Equipped, heres the function:
local function getItemStatus(player,itemName)
local playerData = data[player.UserId]
if table.find(playerData.SelectedTowers, itemName) then
return "Equipped"
elseif table.find(playerData.OwnedTowers, itemName) then
return "Owned"
end
end
whats weird is that it changes on the server but not the client, even though its the exact same.
This is the code:
local function interactItem(itemName)
local data = interactItemFunc:InvokeServer(itemName)
if data then
playerData = data
updateItems()
end
end
function updateItems()
for i, v in pairs(inventoryFrame:GetChildren()) do
if v:IsA("ImageButton") and v.Name ~= "Template" then
v:Destroy()
end
end
for i, key in pairs(playerData.OwnedTowers) do
local tower = getTowerByKey(key)
local newButton = template:Clone()
newButton.Name = tower.Name
newButton.Image = tower.ImageAsset
newButton.Title.Text = tower.Name
newButton.Parent = inventoryFrame
local towerfromRs = replicatedStorage.Towers:FindFirstChild(tower.Name)
newButton.Price.Text = towerfromRs.Config.Price.Value
newButton.Visible = true
local status = getItemStatus(tower.Name)
print(status,tower.Name)
if status == "Equipped" then
newButton.Checkmark.Visible = true
newButton.LayoutOrder = towerfromRs.Config.Price.Value
elseif status == "Owned" then
newButton.Checkmark.Visible = false
newButton.LayoutOrder = (towerfromRs.Config.Price.Value) + 999999999
end
newButton.Activated:Connect(function()
interactItem(tower.Name)
end)
end
end
The data will change on the server-side when I do this
replicatedStorage.InteractItem.OnServerInvoke = function(player, itemName)
local shopItem = getTowerByKey(itemName)
local playerData = data[player.UserId]
if shopItem and playerData then--if it exists and player has some data
local status = getItemStatus(player,itemName)
if status == "Owned" then
--equip the tower
table.insert(playerData.SelectedTowers, shopItem)
print(playerData)
if #playerData.SelectedTowers > playerData.MaxTowers then
table.remove(playerData.SelectedTowers, 1)
end
elseif status == "Equipped" then
--unselect(if more that 1 selected)
if #playerData.SelectedTowers > 1 then
local towerToRemove = table.find(playerData.SelectedTowers, itemName)
table.remove(playerData.SelectedTowers, towerToRemove)
end
end
return playerData
else
warn("Tower/player data does not exist")
end
return false--if something went wrong
end
But when I print it on the client, the status doesn’t change, if I try to select a tower, it will just stay “Owned” no matter what, any tips?