Gun pickup problem

I am attempting with making a L4D like weapon pickup system with my game. I have achieved porting weapons into player’s camera and weapon switching so far.

I have tried many methods, searched for hours on varied forums, yet neither found any satisfactory solution nor a fitting topic about it.

Here is my code:

local replicatedStorage = game:GetService('ReplicatedStorage')
local UIS = game:GetService('UserInputService')

local loadoutUpdate = replicatedStorage.Events:WaitForChild('LoadoutUpdate')

local primary
local secondary = 'Glock17'
local weaponInHand

loadoutUpdate.OnClientEvent:Connect(function(backpack)
	if primary ~= backpack[1] then
		primary = backpack[1]
		setup(primary)
	elseif secondary ~= backpack[2] then
		secondary = backpack[2]
		setup(secondary)
	end
end)

function setup(weapon)
	if weapon ~= nil then
		print(weapon)
		weaponInHand = replicatedStorage.Weapons:WaitForChild(weapon)
		print("Now equipping: ", weaponInHand)
	end
end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		setup(primary)
	elseif input.KeyCode == Enum.KeyCode.Two then
		setup(secondary)
	end
end)

setup(primary)

When a new gun is picked up, the Primary and Secondary variable work fine as the ‘weapon’ variable shows the name of the current weapon, but the game cannot find the script/model with the exact name no matter if model I’m trying to reach has been already inside ReplicatedStorage. This is what I always get in return:


Can someone help me finding a solution, or at least, a hint for this? Thanks for all of your suggestions.

Are you sure the name of the object you are trying to find is “Instance”? Maybe Instance is a variable, if its that, remove the ".

The object I am calling is a “model”, and I do not use the quotation mark for that. Is it any mistake?

Can you try changing it into “ReplicatedStorage.Weapons[weapon]”? and show me the error?

Here you are

Actually what it is the weapon variable? string, boolean, intance?

It’s expecting a string and you’re giving it an instance (like the model itself). Try doing this:

replicatedStorage.Weapons:WaitForChild(weapon.Name)
3 Likes