This script sets up a system where players can click on items to equip them, and it shows the equipped item. However, when the script is disconnected, it stops working, which is understandable because it is being disconnected. Is there a way for the players to be able re-equipping the item after clickConnection:Disconnect()
?
What Happens
In the video everything works correctly but I want to know how to make it so the ItemButtons can still work after the disconnect
I have this script (client)
This script manages a user interface for item storage, allowing players to equip items by clicking on them. It listens for events from the server, updates the UI based on received data, and handles interactions such as equipping items with corresponding button clicks.
-- GETTING REQUIRED SERVICES AND OBJECTS
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StorageEvent = ReplicatedStorage:WaitForChild("StorageEvent")
-- REFERENCES TO UI ELEMENTS
local StorageFrame = script.Parent.StorageFrame
local MainFrame = StorageFrame.MainFrame
local InfoFrame = StorageFrame.InfoFrame
-- TRACKING CURRENT SPOT INDEX FOR ITEM PLACEMENT
local currentSpotIndex = 1
-- FUNCTION TO DESTROY UI STROKE FROM ITEM BUTTON
local function destroyUIStroke(spotFrame, itemName)
-- CHECK IF UI STROKE EXISTS AND DESTROY IT
local itemButton = spotFrame:FindFirstChild(itemName)
if itemButton and itemButton:IsA("GuiButton") and itemButton:FindFirstChild("UIStroke") then
itemButton.UIStroke:Destroy()
end
end
-- FUNCTION EXECUTED WHEN EQUIP BUTTON IS CLICKED
local function onEquipButtonClick(itemButton, itemName)
return function()
-- REMOVE UI STROKE FROM OTHER SPOT FRAMES
local spotFrames = StorageFrame.SpotFrames:GetChildren()
for _, spotFrame in ipairs(spotFrames) do
destroyUIStroke(spotFrame, itemName)
end
-- REMOVE EXISTING ITEM BUTTON AND UI STROKE
local existingItemButton = MainFrame:FindFirstChild(itemName)
if existingItemButton then
existingItemButton:Destroy()
return
end
-- CLONE AND SET UI STROKE, THEN MOVE ITEM TO MAIN FRAME
local newUIStroke = script.UIStroke:Clone()
newUIStroke.Parent = itemButton
itemButton:Clone().Parent = MainFrame
-- TOGGLE VISIBILITY OF INFO AND CLOSE BUTTONS
InfoFrame.Visible = false
StorageFrame.CloseTextButton.Visible = true
StorageFrame.CloseTextButtonB.Visible = false
end
end
-- LISTEN TO CLIENT EVENTS FROM SERVER
StorageEvent.OnClientEvent:Connect(function(itemName, value)
if value then
-- GET OR CLONE ITEM BUTTON BASED ON NAME
local itemButton = MainFrame:FindFirstChild(itemName) or script.Template:Clone()
itemButton.Name = itemName
-- PLACE ITEM BUTTON IN THE CURRENT SPOT FRAME
local currentSpot = StorageFrame.SpotFrames["Spot" .. currentSpotIndex]
itemButton.Parent = currentSpot
currentSpotIndex = currentSpotIndex % #StorageFrame.SpotFrames:GetChildren() + 1
-- HANDLE ITEM BUTTON CLICK EVENT
local clickConnection
clickConnection = itemButton.MouseButton1Click:Connect(function()
-- REFERENCES TO INFO ELEMENTS
local nameTextLabel = InfoFrame.NameTextLabel
local equipButton = InfoFrame.EquipButton
-- TOGGLE VISIBILITY OF ELEMENTS AND SET NAME LABEL
StorageFrame.CloseTextButton.Visible = false
StorageFrame.CloseTextButtonB.Visible = true
nameTextLabel.Text = itemButton.Name
InfoFrame.Visible = true
-- CONNECT EQUIP BUTTON CLICK EVENT
equipButton.MouseButton1Click:Connect(onEquipButtonClick(itemButton, itemName))
-- DISCONNECT CLICK EVENT TO PREVENT MULTIPLE CONNECTIONS
clickConnection:Disconnect()
end)
end
end)
And This Script (Server)
This script connects a function to the event of a player joining the game. Upon joining, it creates a new folder named “Storage” in the player’s instance, and whenever a child is added to this folder, it fires a client event (StorageEvent) to inform the player about the added item, sending the item’s name and a boolean value true.
local storageEvent = game.ReplicatedStorage.StorageEvent
game.Players.PlayerAdded:Connect(function(Player)
local storage = Instance.new("Folder", Player)
storage.Name = "Storage"
storage.ChildAdded:Connect(function(Item)
storageEvent:FireClient(Player, Item.Name, true)
end)
end)