Hi! So I just made this loadout system and what I want it to do is when there is a starter weapon (both primary and secondary) and that the rest of the weapons are locked. Thoughts and suggestions are appreciated.
The local script in the GUI:
local camera = workspace.CurrentCamera
local event = game.ReplicatedStorage.Events:WaitForChild("EquipWeapon")
local weapons = game.ReplicatedStorage:WaitForChild("Weapons")
local weaponTypes = {weapons:WaitForChild("Primary"), weapons:WaitForChild("Secondary")}
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadoutGui = script.Parent:WaitForChild("Loadout")
local spawnButton = loadoutGui.Play
local primaryWeapon = nil
local secondaryWeapon = nil
local tweenService = game:GetService("TweenService")
local speed = TweenInfo.new(.5)
local function updateLoadout()
if primaryWeapon then
event:FireServer(primaryWeapon)
end
if secondaryWeapon then
event:FireServer(secondaryWeapon)
end
end
local function selectWeapon(button, color1, color2)
button.BackgroundColor3 = color1
tweenService:Create(button, speed, {BackgroundColor3 = color2}):Play()
end
updateLoadout()
humanoid.WalkSpeed = 0
humanoid.JumpHeight = 0
for _, weaponType in pairs(weaponTypes) do
local button = script.WeaponType:Clone()
button.Text = weaponType.Name
button.Parent = loadoutGui.WeaponType
button.MouseButton1Click:Connect(function()
for i, v in pairs(loadoutGui.WeaponList:GetChildren()) do
if v:IsA("ImageButton") then v:Destroy() end
end
loadoutGui.TextLabel.Visible = false
for _, weapon in pairs(weaponType:GetChildren()) do
local weaponButton = script.Weapon:Clone()
weaponButton:WaitForChild("WeaponName").Text = weapon.Name
weaponButton.Parent = loadoutGui.WeaponList
weaponButton.Image = "rbxassetid://"..weapon:WaitForChild("ImageID").Value
weaponButton.LayoutOrder = weapon:WaitForChild("Order").Value
weaponButton.MouseButton1Click:Connect(function()
selectWeapon(weaponButton, Color3.fromRGB(0, 255, 0), Color3.fromRGB(0, 0, 85))
if weaponType.Name == "Primary" then
primaryWeapon = weapon
elseif weaponType.Name == "Secondary" then
secondaryWeapon = weapon
end
updateLoadout()
end)
end
end)
end
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = workspace:FindFirstChild("SpawnCamPart").CFrame
camera.CameraSubject = workspace:FindFirstChild("SpawnCamPart")
spawnButton.MouseButton1Click:Connect(function()
if primaryWeapon == nil or secondaryWeapon == nil then
spawnButton.Text = "A primary/secondary weapon must be selected."
wait(3)
spawnButton.Text = "SPAWN"
else
humanoid.WalkSpeed = 16
humanoid.JumpHeight = 7.2
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = player.Character
script.Parent.Enabled = false
end
end)
The server script in ServerScriptService:
local event= game.ReplicatedStorage.Events:WaitForChild("EquipWeapon")
local weapons = game.ReplicatedStorage:WaitForChild("Weapons")
event.OnServerEvent:Connect(function(player, weapon)
if weapon and weapon.Parent.Parent == weapons then
local weaponType = weapon.Parent
for _, tool in pairs(player.Backpack:GetChildren()) do
if weaponType:FindFirstChild(tool.Name) then
tool:Destroy()
end
end
for _, tool in pairs(player.Character:GetChildren()) do
if weaponType:FindFirstChild(tool.Name) then
tool:Destroy()
end
end
local weaponClone = weapon:Clone()
weaponClone.Parent = player:WaitForChild("Backpack")
end
end)