Problems with Equipping Items in My Inventory System

I have an inventory system to hold “Jokes”. Although, you are only able to have 3 jokes equipped at a time. But currently there is a visual bug where you are able to equip more than 3 jokes. When it says you have 3 equipped and you click on another joke all your jokes in the “EquippedJokes” folder of the player disappear, but it still visually shows you have 3 equipped. Then you can proceed to equip more than 3 visually for some reason.

Here is the full code that manages the jokes.
Local Script:

local ts = game:GetService("TweenService")

local buttonConnections = {}

local click = script.Parent.Parent.Click
local player = game.Players.LocalPlayer

local deleting = false



local function updateStatsFrame(joke)
	local frame = player.PlayerGui.ScreenGui.JokeInv.Stats
	frame.Visible = true
	frame.Title.Text = joke.Name 
	frame.Icon.Image = "rbxthumb://type=Asset&id=" .. tostring(joke.Icon.Value) .. "&w=420&h=420"
	frame.Laughs.Text = "Laughs  +"..tostring(joke.Laughs.Value)
	frame.Fans.Text = "Fans  +"..tostring(joke.Fans.Value)
	frame.AbilityDesc.Text = tostring(joke.Desc.Value)
end

local function DisplayNewJoke(joke, loaded)
	local frame = player.PlayerGui.ScreenGui.Joke
	local info = TweenInfo.new(1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
	local goals = {Size = UDim2.new(0.218, 0,0.483, 0)}
	local tween = ts:Create(frame, info, goals)
	
	if not loaded then
		frame.Visible = true
		game.ReplicatedStorage.RemoteEvents.LocallyNotify:FireServer("Unlocked " .. joke.Name .. "!", 3)
		frame.Title.Text = joke.Name
		frame.Icon.Image = "rbxthumb://type=Asset&id=" .. tostring(joke.Icon.Value) .. "&w=420&h=420"
		frame.Laughs.Text = "Laughs  +"..tostring(joke.Laughs.Value)
		frame.Fans.Text = "Fans  +"..tostring(joke.Fans.Value)
		frame.Ability.Text = tostring(joke.Desc.Value)
		tween:Play()
		end
	end

local function unequipJoke(joke, icon)
	for i, v in pairs(player.EquippedJokes:GetChildren()) do
		if v.Name == joke.Name then
			icon.Visible = false
			v:Destroy()
		end
	end
end

local function equipJoke(joke,icon)
	if #player.EquippedJokes:GetChildren() >= 1 and #player.EquippedJokes:GetChildren() <= player.HiddenStats.MaxJokes.Value then
				local newJoke = Instance.new("IntValue", player.EquippedJokes)
				newJoke.Name = joke.Name
			if #player.EquippedJokes:GetChildren() >= player.HiddenStats.MaxJokes.Value + 1 then
					unequipJoke(joke, icon)
					player.PlayerGui.ScreenGui.JokeInv.Stats.Visible = false
			end
				else
			local newJoke = Instance.new("IntValue", player.EquippedJokes)
			newJoke.Name = joke.Name
			updateStatsFrame(joke)
		end
	
	end

local function addToFrame(joke)
	local newTemp = game.ReplicatedStorage.Temp:Clone()
	newTemp.Name = joke.Name
	newTemp.Icon.Image = "rbxthumb://type=Asset&id=" .. tostring(joke.Icon.Value) .. "&w=420&h=420"
	newTemp.Stat.Text = tostring(joke.Laughs.Value)
	newTemp.Parent = script.Parent.ScrollingFrame
	
	buttonConnections[#buttonConnections+1] = newTemp.Icon.MouseButton1Click:Connect(function()
		
		if deleting then
			player.PlayerGui.ScreenGui.JokeInv.ScrollingFrame[joke.Name]:Destroy()
			local jokeInInv = player.JokeInv[joke.Name] or player.EquippedJokes[joke.Name]
			jokeInInv:Destroy()
			game.SoundService.Sounds.Trash:Play()
		end
		
		if newTemp.Equipped.Visible and not deleting  then
			unequipJoke(joke, newTemp.Equipped)
			player.PlayerGui.ScreenGui.JokeInv.Stats.Visible = false
			newTemp.Equipped.Visible = false
			click:Play()
		else if not newTemp.Equipped.Visible and #player.EquippedJokes:GetChildren() < player.HiddenStats.MaxJokes.Value + 1 and not deleting then
			newTemp.Equipped.Visible = true
			click:Play()
			equipJoke(joke,newTemp.Equipped)
			updateStatsFrame(joke)
			end
		end
	end)
end

script.Parent.Trash.MouseButton1Click:Connect(function()
	if deleting then
		deleting = false
		script.Parent.Trash.ImageColor3 = Color3.new(1, 1, 1)
	else
		deleting = true
		script.Parent.Trash.ImageColor3 = Color3.new(1, 0, 0.0156863)
	end
end)

game.ReplicatedStorage.RemoteEvents.Joke.AddToFrame.OnClientEvent:Connect(function(joke, loaded)
	DisplayNewJoke(joke, loaded)
	addToFrame(joke)
end)

Server Script

game.ReplicatedStorage.RemoteEvents.GiveJoke.OnServerEvent:Connect(function(player, joke)
	local canGive = false
	local newJoke = Instance.new("IntValue", player.JokeInv)
	newJoke.Name = joke
	
	for i, v in pairs(game.ReplicatedStorage.Jokes:GetChildren()) do
		if v.Name == joke then
			game.ReplicatedStorage.RemoteEvents.Joke.AddToFrame:FireClient(player, v, false)
			break
		end
	end
end)

Also I thought it would be worth mentioning that I this only happens if you have multiple of the same joke, although I would like you to have multiple of the same joke.
If you need clarification on the code, please let me know

2 Likes