Problem with custom inventory system

I am currently developing an inventory system, that shouldn’t cover alot of the player’s fov (invisible). The results should look similar to the ones you see in alot of shooter games. For that, I use folders in the replicated storage, that have the player name they belong to. They also contain a folder for the primary, and one for the secondary weapon, and two remote events, one for equiping the primary, and one for equipping the secondary.

The issue is, the code won’t parent the weapon to the player, and even if, the tool probably won’t work.
Here are the most important parts of the code:
Equip_WeaponData (Serverscript in a screengui):

    wait()

--//Variables for Loadout lol
local plrname = script.Parent.Parent.Parent.Name
local LoadOutPlr = game.ReplicatedStorage[plrname]
local LoadOutFolder = LoadOutPlr.LoadOutFolder
local Pr = LoadOutFolder.Primaries
local Sr = LoadOutFolder.Secondaries

--//Now ill get to the serverside stuff ill need
local Character = workspace[plrname]
local Humanoid = Character:WaitForChild("Humanoid")
local PrWeapon = Pr:FindFirstChildOfClass("Tool")
local SrWeapon = Sr:FindFirstChildOfClass("Tool")

--//Creating some events and stuff
local Pequip = Instance.new("RemoteEvent", LoadOutPlr)
local Sequip = Instance.new("RemoteEvent", LoadOutPlr)

--//Naming the events so the client script that detects if the player presses 1 or 2 can find the events
Pequip.Name = "PrimaryEquip"
Sequip.Name = "SecondaryEquip"

----------------------------------------------
----------------------------------------------

--//Now we'll check for the events, main code
Pequip.OnServerEvent:Connect(function()
	local Tool = Character:FindFirstChildOfClass("Tool")
	
	if Tool and Humanoid then
		Tool:Destroy() --force unequips the tool
		Humanoid:EquipTool(PrWeapon) --equips new tool
	end
	
end)

Sequip.OnServerEvent:Connect(function()
	local Tool = Character:FindFirstChildOfClass("Tool")

	if Tool and Humanoid then
		Tool:Destroy()
		Humanoid:EquipTool(SrWeapon)
	end
	
end)

Equip_WeaponClient (Localscript in screengui):

repeat wait() until script.Parent.Enabled == false --player cant equip a gun, unless he already deployed
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
--//locate the name of the player
local plrname = script.Parent.Parent.Name

--//the events we created in "Equip_WeaponData"
local Pequip = game.ReplicatedStorage:WaitForChild(plrname):WaitForChild("PrimaryEquip")
local Sequip = game.ReplicatedStorage:WaitForChild(plrname):WaitForChild("SecondaryEquip")

--//now we'll get the user input service rolling
local Uis = game:GetService("UserInputService")

----------------------------------------------
----------------------------------------------

Uis.InputBegan:Connect(function(io, p)
	if io.KeyCode == Enum.KeyCode.One then --if the player presses 1, we'll fire the remote event
		Pequip:FireServer() 
	end
	
	if io.KeyCode == Enum.KeyCode.Two then --the player also might want to equip a secondary, so we also make a code for that
		Sequip:FireServer()
	end
end)

If you need more code, like the code that creates the folders, then just tell me and I’ll happily provide more. Like I said, the tools won’t equip.

I tried rewriting the code multiple times in multiple services. The output doesnt say anything.
Also I’ve never done a system that is too similiar to this, so I am not very experienced in coding custom inventory systems yet.

Energized WIP - Roblox Studio 20.08.2021 12_01_31 (2)

1 Like

This is something wrong from the beginning. You should make main manager scripts which’ll work with events instead of this.

So basically what you do here is, if there’s a tool which is already equipped, you destroy the tool and make it equip a new one. However what if they don’t have a gun equipped from the beginning? I don’t know if you start players with a gun but if not, this is your issue.

2 Likes

Let me just think about this @Dyzody .

First of all, on the server side, when recieving a remote event, you check if there are any tools in the character of the player, and if there aren’t any, you won’t give the tool?

Also, if this server script is replicated into every players gui, then when any client fires the remote event, every client will recieve the request to change their loadout. The way this script is written isn’t the most efficient, but it does the job.

An Example (Might Be Better)

Server Side

Just to sum up the things I did here.

  • I made a table containing every players loadouts of choice (or deafult values) and their previously equipped weapon
  • There are only two events now, instead of every player having their own pair (Reference Image 1)
  • The weapons are all moved to the same folder now (Reference Image 1)
  • Every time an event is sent by the client, the server checks if that player has sent that an event before. If they haven’t, then we create a table for them.
  • We clone the players preffered primary or secondary weapon into their character and equip it.

Reference Image 1
orientation

local userChoices = {} -- table conataining every players loadout data

local deafultPrimary = "AK-47" -- name of a weapon in the weapons folder
local deafultSecondary = "Knife" -- name of a weapon in the weapons folder

local Pequip = game.ReplicatedStorage.Events.PrimaryWeapon -- primary equipping event
local Sequip = game.ReplicatedStorage.Events.SecondaryWeapon -- secondary equipping event

local function createPreferenceTable(playerName) -- function that creates a new table for the player
	
	userChoices[playerName] = {

		Primary = deafultPrimary,
		Secondary = deafultSecondary,
		LastWeapon = nil

	}
	
end

local function equipWeapon(selectedWeapon, humanoid, character, player)
	
	if game.ReplicatedStorage.Weapons:FindFirstChild(selectedWeapon) then

		if game.ReplicatedStorage.Weapons:FindFirstChild(selectedWeapon).ClassName ~= "Tool" then return false end
		
		if userChoices[player.Name].LastWeapon then -- does the player have another weapon equipped
			
			userChoices[player.Name].LastWeapon:Destroy() -- destroy equipped weapon
			
		end

		local weapon = game.ReplicatedStorage.Weapons:FindFirstChild(selectedWeapon):Clone() -- clone the preffered weapon
		weapon.Parent = character -- set the weapons parent to the players character

		humanoid:EquipTool(weapon) -- equip the preffered tool

		userChoices[player.Name].LastWeapon = weapon -- set the current tool to be deleted next time an event is called

	end
	
end

Pequip.OnServerEvent:Connect(function(player)
	
        -- do we have a character and humanoid
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	
	if not character then return false end
	if not humanoid then return false end

	if not userChoices[player.Name] then -- has the player called these events before
		
		createPreferenceTable(player.Name)
		
		equipWeapon(userChoices[player.Name].Primary, humanoid, character, player)
		
		
	else
		
		equipWeapon(userChoices[player.Name].Primary, humanoid, character, player)
		
	end

end)

Sequip.OnServerEvent:Connect(function(player)
	
        -- do we have a character and humanoid
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if not character then return false end
	if not humanoid then return false end

	if not userChoices[player.Name] then -- has the player called these events before

		createPreferenceTable(player.Name)
		
		equipWeapon(userChoices[player.Name].Secondary, humanoid, character, player)

	else

		equipWeapon(userChoices[player.Name].Secondary, humanoid, character, player)

	end

end)

If you should wish to change the preferences of a player, then this is how I would do it. “Choice”, of course being the name of a weapon in the weapons folder

*(changePrimaryWeaponEvent)*.OnServerEvent:Connect(function(player, choice)

	if not userChoices[player.Name] then

		createPreferenceTable(player.Name)

		if choice then

			if game.ReplicatedStorage.Weapons:FindFirstChild(tostring(choice)) then

				userChoices[player.Name].Primary = choice
				return true

			end

		end
		return false
	end
	
	if choice then

		if game.ReplicatedStorage.Weapons:FindFirstChild(tostring(choice)) then

			userChoices[player.Name].Primary = choice
			return true

		end

	end

end)
1 Like

Hey there, thanks for the reply. I should’ve mentioned it before, but the player starts with a gun after deployment.

Thank you for replying! The players starts with a two guns, so the player wont get a tool if he didn’t select a primary. The menu checks if he has one when deploying.

Like I said, I am not too experienced with writing custom inventories.

Your code helped out, thank you! Sadly there’s still the problem that the player gets the gun, but it doesnt show up, and it’s scripts don’t seem to load, is there any known way of fixing this?

What I want to say: i want the player to be able to modify their guns to their needs. That is why i made a folder for every player.

Doing it that way is just inefficient. You should, for example, get the modifications from a datastore and then add them to the desired weapon.