Won't let me insert items with the same StringValue

  1. What do you want to achieve?
    I want it so that once the player inserts an item into a slot, only the items that have the same Type.Value will be displayed in the Items grid and can be inserted into the other slots.

  2. What is the issue?
    When I insert an item, it won’t let me insert another item with the same Type.Value into the next slot.

  3. What solutions have you tried so far?
    I’ve tried re-writing my code multiple times; however, it doesn’t work and I get no output errors.

Here are some screenshots:



Script:

--\\ VARIABLES //--
local DataFolder = game.Players.LocalPlayer:WaitForChild('Data_Folder')
local SettingsFolder = script.Parent.Parent.Parent.Parent:WaitForChild('GameInventory').Frame.Inventory.Settings
local ForgeFrame = script.Parent.ForgeFrame
local UpgradeButton = script.Parent.ForgeFrame.UpgradeButton
local OpenEvent = game.ReplicatedStorage.UpgradeEvents.OpenTradeUI -- create RE
local UpgradeCard = game.ReplicatedStorage.UpgradeEvents.SubmitArtifacts -- create RE
local ItemGrid = script.Parent.Items.Grid
local InvGrid = script.Parent.Parent.Parent.Parent.GameInventory.Frame.Inventory.Items.Grid
local Events = game.ReplicatedStorage.InvEvents
local Available = ForgeFrame.Slots.Available --// Holds the value of the first available slot

local Slots = {
	[1] = ForgeFrame.Slots:FindFirstChild('1'),
	[2] = ForgeFrame.Slots:FindFirstChild('2'),
	[3] = ForgeFrame.Slots:FindFirstChild('3'),
	[4] = ForgeFrame.Slots:FindFirstChild('4')
}

local selectedType = nil -- Track the selected item type

--\\ FUNCTIONS //--
local function addToInv(item)
	local template = InvGrid.Parent.Template:Clone()
	template.Name = tostring(item)
	template.Object.Text = tostring(item)

	for i, x in pairs(game.ReplicatedStorage:GetChildren()) do
		if x:IsA("Folder") and x.Name == "Artifacts" then
			local model = x:FindFirstChild(item)
			if model and model:IsA("Model") and model:FindFirstChild("Type") and model.Type.Value == selectedType then
				model = model:Clone()
				model.Parent = template.ViewportFrame.Model
				template.Desc.Value = model.Desc.Value
				break
			end
		end
	end

	template.RotateSc.Disabled = false
	template.Parent = InvGrid
	template.Visible = true
end

local function displayCard(itemSlab)
	local item = itemSlab.ViewportFrame.Model:FindFirstChildWhichIsA('Model')

	local newCard = item:Clone()
	newCard.Parent = Slots[Available.Value].ViewportFrame.Model

	Slots[Available.Value].Occupied.Value = itemSlab.Name
	itemSlab.Visible = false
	Available.Value += 1

	for _, tab in ipairs(ItemGrid:GetChildren()) do
		if tab:IsA('Frame') then
			local itemModel = tab.ViewportFrame.Model:FindFirstChildWhichIsA('Model')
			if itemModel and itemModel:FindFirstChild("Type") and itemModel.Type.Value == selectedType then
				if tab.Name ~= item.Name then
					tab.Visible = false
				end
			end
		end
	end
end

local function insertCard(itemSlab)
	if Available.Value > 1 then
		if itemSlab.Name == Slots[1].Occupied.Value then
			displayCard(itemSlab)
		end
	else
		displayCard(itemSlab)
	end
end

local function removeCard(slot)
	local item = slot.ViewportFrame.Model:FindFirstChildWhichIsA('Model')
	if item then
		item:Destroy()
		slot.Occupied.Value = ''
		Available.Value -= 1

		for _, itemSlab in ipairs(ItemGrid:GetChildren()) do
			if itemSlab:IsA('Frame') and itemSlab.Name == item.Name then
				if itemSlab.Visible == false then
					itemSlab.Visible = true
					break
				end
			end
		end
	end
end

local function update(tab, state)
	if state == 'add' then
		local template = script.Parent.Items.Template:Clone()
		template.Name = tab.Name
		template.Object.Text = tab.Name

		local model = tab.ViewportFrame.Model:FindFirstChildWhichIsA('Model'):Clone()
		model.Parent = template.ViewportFrame.Model

		template.RotateSc.Disabled = false
		template.Parent = script.Parent.Items.Grid
		template.Visible = true

		template.SelectButton.Activated:Connect(function()
			if Available.Value <= 4 then
				insertCard(template)
			end
		end)

	elseif state == 'remove' then
		local template = script.Parent.Items.Grid:FindFirstChild(tab.Name)
		if template then
			template:Destroy()
		end
	end
end

local function updateList()
	for _, tab in ipairs(ItemGrid:GetChildren()) do
		if tab:IsA('Frame') then
			update(tab, 'remove')
		end
	end

	for _, tab in ipairs(InvGrid:GetChildren()) do
		if tab:IsA('Frame') then
			if selectedType then
				local itemModel = tab.ViewportFrame.Model:FindFirstChildWhichIsA('Model')
				if itemModel and itemModel:FindFirstChild("Type") and itemModel.Type.Value == selectedType then
					update(tab, 'add')
				end
			else
				update(tab, 'add')
			end
		end
	end
end

local function upgradeCard(cardName: string)
	local newCard = tostring(cardName..'I')
	local levels = {}

	for x = 1, 4 do
		local card = nil

		-- Find a card with the matching level
		for _, slot in ipairs(Slots) do
			local item = slot.ViewportFrame.Model:FindFirstChildWhichIsA('Model')
			if item and item:FindFirstChild("Level") and item.Level.Value == x then
				card = item
				break
			end
		end

		if card then
			levels[x] = true
		else
			levels[x] = false
		end
	end

	local allLevelsFilled = true
	for _, filled in ipairs(levels) do
		if not filled then
			allLevelsFilled = false
			break
		end
	end

	if allLevelsFilled then
		for _, slot in ipairs(Slots) do
			removeCard(slot)
		end

		Events.AddToInv:FireServer(newCard)
		addToInv(newCard)
		update(InvGrid:FindFirstChild(newCard), 'add')
		script.UpgradeSound:Play()

		SettingsFolder.CurentArtStorage.Value -= 2

		Available.Value = 1
	else
		print("Please fill all slots with the required levels.")
	end
end

--\\ EVENTS //--
OpenEvent.OnClientEvent:Connect(function(itemType)
	script.Parent.Parent.Visible = true
	selectedType = itemType
	updateList()
end)

for _, slot in ipairs(Slots) do
	slot.Activated:Connect(function()
		if slot.Occupied.Value ~= '' and slot.Occupied.Value ~= nil then
			if slot.ViewportFrame.Model:FindFirstChildOfClass('Model') then
				removeCard(Slots[Available.Value - 1])
			end
		end
	end)



	slot.Text = slot.Occupied.Value
	slot.Occupied.Changed:Connect(function(value)
		slot.Text = value
	end)
end

Available.Changed:Connect(function(value)
	if value == 1 then
		for _, tab in ipairs(ItemGrid:GetChildren()) do
			if tab:IsA('Frame') then
				tab.Visible = true
			end
		end
	end
end)

UpgradeButton.Activated:Connect(function()
	upgradeCard(Slots[1].Occupied.Value)
	task.wait(0.1)
	updateList()
end)

game.ReplicatedStorage.UpgradeEvents.OpenTradeUI.OnClientEvent:Connect(function(itemType)
	if DataFolder.CurrentArtStorage.Value ~= (#ItemGrid:GetChildren() - 1) then
		selectedType = itemType
		updateList()
	end
end)

task.wait(3)
updateList()

Let me know if you need any more info to help me solve my issue :slight_smile:

1 Like