You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
When the player wants to equip and item, it’ll only give them one of that item instead of multiple of the same item. -
What is the issue? Include screenshots / videos if possible!
If the player were to switch back and forth items in the shop, it’ll add new connections which would then give the player multiple of the same item if they were to equip an item.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried making thenameVal:Changed
function a connection to try and disconnect older connections but couldn’t figure out how to and where to place the:Disconnect
events. I also tried to see if anyone had the same problem as me on the DevForums but nothing similar.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- Services
local RS = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
-- REs
local REFolder = RS:WaitForChild("RemoteEvents").shopSystem
-- Variables
local player = players.LocalPlayer
local mainFrame = script.Parent.Parent
local frames = mainFrame.Frames
local homeFrame = frames:FindFirstChild("1_homeFrame")
local gunsFrame = frames:FindFirstChild("2_gunsFrame")
local meleeFrame = frames:FindFirstChild("3_meleeFrame")
local miscFrame = frames:FindFirstChild("4_miscFrame")
local infoFrame = mainFrame:FindFirstChild("infoFrame")
local buyButton = infoFrame.buyButton
-- Values
local nameVal = infoFrame.nameValue
local priceVal = infoFrame.priceValue
-- Local functions
local function clearViewFrame()
for i, v in pairs(infoFrame.itemViewFrame:GetDescendants()) do
if v:IsA("ViewportFrame") then
v:Destroy()
end
end
end
local function updateItems(frame)
local pOI = player:WaitForChild("OwnedItems")
pOI.ChildAdded:Connect(function(newItem)
warn("Item: ".. newItem.Name .." || Frame: ".. frame.Name)
for i, v in pairs(frame:GetDescendants()) do
if v:IsA("TextLabel") and v.Name == "itemPrice" then
local item = v.Parent
if newItem.Name == item.itemName.Text then
local priceLabel = item.itemPrice
priceLabel.Text = "OWNED"
priceLabel.TextColor3 = Color3.fromRGB(75, 255, 75)
priceLabel.UIStroke.Color = Color3.fromRGB(80, 175, 80)
end
end
end
end)
end
local function updateBuyButton()
if player:WaitForChild("OwnedItems"):FindFirstChild(nameVal.Value) then
if not (player.Backpack:FindFirstChild(nameVal.Value) or player.Character:FindFirstChild(nameVal.Value)) then
buyButton.Text = "EQUIP"
buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)
elseif player.Backpack:FindFirstChild(nameVal.Value) or player.Character:FindFirstChild(nameVal.Value) then
buyButton.Text = "UNEQUIP"
buyButton.BackgroundColor3 = Color3.fromRGB(255, 165, 110)
buyButton.borderStroke.Color = Color3.fromRGB(125, 80, 54)
end
else
buyButton.Text = "PURCHASE"
buyButton.BackgroundColor3 = Color3.fromRGB(90, 255, 105)
buyButton.borderStroke.Color = Color3.fromRGB(44, 125, 51)
end
end
-- Loops to update the infoFrame information
for i,v in pairs(meleeFrame:GetDescendants()) do
if v:IsA("TextButton") and v.Name == "itemButton" then
local item = v.Parent
v.MouseButton1Click:Connect(function()
infoFrame:FindFirstChild("itemViewFrame").Visible = true
infoFrame:FindFirstChild("buyButton").Visible = true
clearViewFrame()
local newViewFrame = item.ViewportFrame:Clone()
newViewFrame.Parent = infoFrame:FindFirstChild("itemViewFrame")
newViewFrame.Position = UDim2.new(0, 0, 0, 0)
newViewFrame.Size = UDim2.new(1, 0, 1, 0)
infoFrame.miscInfo:FindFirstChild("1_name").Text = "Name: ".. item:FindFirstChild("_Name").Value
infoFrame.miscInfo:FindFirstChild("2_description").Text = "Description: ".. item:FindFirstChild("_Description").Value
infoFrame.miscInfo:FindFirstChild("3_price").Text = "C$".. item:FindFirstChild("_Price").Value
nameVal.Value = item:FindFirstChild("_Name").Value
priceVal.Value = item:FindFirstChild("_Price").Value
end)
end
end
for i,v in pairs(miscFrame:GetDescendants()) do
if v:IsA("TextButton") and v.Name == "itemButton" then
local item = v.Parent
v.MouseButton1Click:Connect(function()
infoFrame:FindFirstChild("itemViewFrame").Visible = true
infoFrame:FindFirstChild("buyButton").Visible = true
clearViewFrame()
local newViewFrame = item.ViewportFrame:Clone()
newViewFrame.Parent = infoFrame:FindFirstChild("itemViewFrame")
newViewFrame.Position = UDim2.new(0, 0, 0, 0)
newViewFrame.Size = UDim2.new(1, 0, 1, 0)
infoFrame.miscInfo:FindFirstChild("1_name").Text = "Name: ".. item:FindFirstChild("_Name").Value
infoFrame.miscInfo:FindFirstChild("2_description").Text = "Description: ".. item:FindFirstChild("_Description").Value
infoFrame.miscInfo:FindFirstChild("3_price").Text = "C$".. item:FindFirstChild("_Price").Value
nameVal.Value = item:FindFirstChild("_Name").Value
priceVal.Value = item:FindFirstChild("_Price").Value
end)
end
end
-- Main script
nameVal.Changed:Connect(function(item)
warn(item)
updateBuyButton()
buyButton.MouseButton1Click:Connect(function(something)
warn(something)
if buyButton.Text == "PURCHASE" then -- Purchasing an item
local price = priceVal.Value
if player.leaderstats.Coins.Value >= price then -- Update the player's leaderstats
warn("Purchased item: ".. nameVal.Value)
buyButton.Text = "EQUIP"
local itemName = nameVal.Value
REFolder.buyItemRE:FireServer(itemName, price)
buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)
for i, v in pairs(frames:GetDescendants()) do
if v:IsA("TextLabel") and v.Name == "itemName" and v.Text == nameVal.Value then
local item = v.Parent
local frame = item.Parent.Parent
updateItems(frame)
end
end
else -- Player is short on coins to buy an item
buyButton.Text = "NOT ENOUGH"
buyButton.BackgroundColor3 = Color3.fromRGB(255, 75, 75)
buyButton.borderStroke.Color = Color3.fromRGB(125, 37, 37)
warn("Can't purchase item: ".. nameVal.Value)
task.wait(1)
buyButton.Text = "PURCHASE"
buyButton.BackgroundColor3 = Color3.fromRGB(90, 255, 105)
buyButton.borderStroke.Color = Color3.fromRGB(44, 125, 51)
end
elseif buyButton.Text == "EQUIP" then -- Equipping an item
warn("Equipped item: ".. nameVal.Value)
local itemName = nameVal.Value
REFolder.equipItemRE:FireServer(itemName)
buyButton.Text = "UNEQUIP"
buyButton.BackgroundColor3 = Color3.fromRGB(255, 165, 110)
buyButton.borderStroke.Color = Color3.fromRGB(125, 80, 54)
elseif buyButton.Text == "UNEQUIP" then -- Unequipping an item
warn("Unequipped item: ".. nameVal.Value)
buyButton.Text = "EQUIP"
buyButton.BackgroundColor3 = Color3.fromRGB(100, 255, 145)
buyButton.borderStroke.Color = Color3.fromRGB(49, 125, 69)
if player.Backpack:FindFirstChild(nameVal.Value) then
player.Backpack:FindFirstChild(nameVal.Value):Destroy()
elseif player.Character:FindFirstChild(nameVal.Value) then
player.Character:FindFirstChild(nameVal.Value):Destroy()
end
end
end)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.