How can i allow the user to select a weapon?

How can i create a system that allows the user to select the Gun, weapon etc?
my current script is:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local CameraPart1 = workspace:WaitForChild("ModCamera"):WaitForChild("Camera")
local CameraPart2 = workspace:WaitForChild("ModCamera"):WaitForChild("LeftCamera")
local CameraPart3 = workspace:WaitForChild("ModCamera"):WaitForChild("RightCamera")
local gun = require(ReplicatedStorage.Data.CurrentGun)

repeat wait() until Player.Character

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CameraPart1.CFrame

script.Parent.Enabled = true

local function showArms()
	Player.PlayerGui.HUD.Enabled = true
	script.Parent.Enabled = false
	
	Camera.CameraType = Enum.CameraType.Custom
	--[[local gun = game.ReplicatedStorage.Guns.M1911:Clone()
	local run = game:GetService("RunService")
	gun.Parent = Camera]]
	Player.PlayerScripts.FireClient.Disabled = false
	--Player.CameraMode = Enum.CameraMode.LockFirstPerson]
	_G.Camera = true
end

local StarterGui = game:GetService("StarterGui")
--[[StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
StarterGui:SetCore("TopbarEnabled", false)]]

local Arrows = script.Parent
local Left = Arrows.Left
local Right = Arrows.Right
local Play = Arrows.Bottom
local canPress = true

local left = {}
left.CFrame = CameraPart2.CFrame

local center = {}
center.CFrame = CameraPart1.CFrame

local right = {}
right.CFrame = CameraPart3.CFrame

local Time = TweenInfo.new(1.5)

Left:WaitForChild('Button').MouseButton1Click:Connect(function()
	if canPress then
		if Camera.CFrame == CameraPart1.CFrame then
			canPress = false
			local tween = TweenService:Create(Camera, Time, left)
			tween:Play()
			wait(1.5)
			canPress = true
		elseif Camera.CFrame == CameraPart3.CFrame then
			canPress = false
			local tween = TweenService:Create(Camera, Time, center)
			tween:Play()
			wait(1.5)
			canPress = true
		else
			canPress = false
			local tween = TweenService:Create(Camera, Time, right)
			tween:Play()
			wait(1.5)
			canPress = true
		end
	end
end)

Right:WaitForChild('Button').MouseButton1Click:Connect(function()
	if canPress then
		if Camera.CFrame == CameraPart1.CFrame then
			canPress = false
			local tween = TweenService:Create(Camera, Time, right)
			tween:Play()
			gun.currentGun = "DEAGLE44"
			wait(1.5)
			canPress = true
		elseif Camera.CFrame == CameraPart2.CFrame then
			canPress = false
			local tween = TweenService:Create(Camera, Time, center)
			tween:Play()
			gun.currentGun = "M1911"
			wait(1.5)
			canPress = true
		else
			canPress = false
			local tween = TweenService:Create(Camera, Time, left)
			tween:Play()
			gun.currentGun = "AWM"
			wait(1.5)
			canPress = true
		end
	end
end)

Play:WaitForChild("Play").MouseButton1Click:Connect(function()
	showArms()
end)

which i will rewrite once i get the time for it because it does not select the gun when i select with the client framework:

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

--// Main Variables
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local currentGun = require(ReplicatedStorage.Data.CurrentGun)

Player.CharacterAdded:Connect(function(Character)
	local function getGun()
		local Gun = ReplicatedStorage.Weapons:FindFirstChild(currentGun.currentGun)
		local Settings = require(ReplicatedStorage.Data[Gun.Name].Settings)
		
		wait(5)
		print(Gun.Name)
		print(Settings.damage)
	end
	
	if currentGun.currentGun ~= "" then
		getGun()
	else
		repeat wait() until not currentGun.currentGun == ""
		getGun()
	end
end)
1 Like

I’m 99% sure that you cant do it like this. I think a better way to do this is to implement RemoteEvents into your code, so you can efficiently talk to the server and the client. Here’s a roblox dev page: https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

2 Likes

how would i do that? would i have to just use a table in a server script because i am currently using a modulescript

What exactly do you mean by ‘select a gun’, do you mean a loadout-esque system or do you mean a custom backpack GUI?

loadout-esque system, similar to the one in games like phantom forces

You can do this on the client, but make sure you have the scripts not reset on spawn so that loadout data is saved. To actually add the guns to the player, make sure you use remote events. If you want the player to save loadouts from their last session, you will also need to create a datastore to store their last used weapon.