Loadout GUI doesn't copy button to Scrolling Frame

i’m making Loadout System, that cloning weapon into player’s inventory.

How is it should work:
Player clicks Primary, Sidearm or Melee slot button. There is Scrolling Frame, and after clicking slot button, the WeaponButton with certain gun name cloning to ScrollingFrame. If player pressing weapon button, then RemoteEvent firing, gun cloning and gets to player’s backpack. Slot’s name should have same name as gun, after choosing weapon buttons.

Problem:
Weapon Button doesn’t show up.

local script:

local menuGui = script.Parent
local loadoutFrame = menuGui.LoadoutFrame
local primaryLabel = loadoutFrame.PrimaryFrame.PrimaryLabel
local sidearmLabel = loadoutFrame.SidearmFrame.SidearmLabel
local meleeLabel = loadoutFrame.MeleeFrame.MeleeLabel
local scrollingFrame = loadoutFrame.ScrollingFrame

local weaponFolder = game:GetService("ServerStorage"):WaitForChild("WeaponFolder")

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("WeaponSelectedEvent")

local currentSlot = nil

local function clearWeaponButtons()
	print("Clearing weapon buttons")
	for _, button in pairs(scrollingFrame:GetChildren()) do
		if button:IsA("TextButton") then
			button:Destroy()
		end
	end
end

local function populateWeaponButtons(slotType)
	print("Populating buttons for slot type: " .. slotType)
	clearWeaponButtons()

	local folder = weaponFolder:FindFirstChild(slotType .. "Folder")
	if folder then
		for _, weapon in pairs(folder:GetChildren()) do
			print("Adding button for weapon: " .. weapon.Name)
			local buttonTemplate = script:WaitForChild("WeaponButton")
			local button = buttonTemplate:Clone()
			button.Text = weapon.Name
			button.Name = weapon.Name
			button.Parent = scrollingFrame
			button.MouseButton1Click:Connect(function()
				if currentSlot then
					currentSlot.Text = weapon.Name
					print("Selected weapon: " .. weapon.Name)
					remoteEvent:FireServer(slotType, weapon.Name)
				end
			end)
		end
	else
		warn("Folder for " .. slotType .. " weapons not found.")
	end
end

primaryLabel.MouseButton1Click:Connect(function()
	print("Primary label clicked")
	currentSlot = primaryLabel
	populateWeaponButtons("Primary")
end)

sidearmLabel.MouseButton1Click:Connect(function()
	print("Sidearm label clicked")
	currentSlot = sidearmLabel
	populateWeaponButtons("Sidearm")
end)

meleeLabel.MouseButton1Click:Connect(function()
	print("Melee label clicked")
	currentSlot = meleeLabel
	populateWeaponButtons("Melee")
end)

Server Script:

local weaponFolder = game:GetService("ServerStorage"):WaitForChild("WeaponFolder")
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("WeaponSelectedEvent")

remoteEvent.OnServerEvent:Connect(function(player, slotType, weaponName)
	print("Received event for " .. slotType .. " with weapon " .. weaponName)
	local folder = weaponFolder:FindFirstChild(slotType .. "Folder")
	if folder then
		local weapon = folder:FindFirstChild(weaponName)
		if weapon then
			local clone = weapon:Clone()
			clone.Parent = player.Backpack
		else
			warn("Weapon " .. weaponName .. " not found in " .. slotType .. "Folder.")
		end
	else
		warn("Folder for " .. slotType .. " weapons not found.")
	end
end)


robloxapp-20240717-1526227.wmv (934.7 KB)

1 Like