[Fixed] FE Gunkit Gun Is Nil

Hello! I’m currently trying to make an attachments system for FE Viewmodel, and it works with tools, here’s the code I’m using to attach attachments to the tool:

local AttachmentsModule = {}

local attachments = game.ReplicatedStorage.Miscs.Attachments

function AttachmentsModule.GiveAttachment(gun,attachment,player)
	local newAttachment = attachment:Clone()
	newAttachment.Parent = gun
	local attachmentBase = newAttachment.AttachmentBase
	local attachmentType = newAttachment.AttachmentInfo.AttachmentType.Value
	newAttachment.Name = attachmentType.."Attachment"
	
	local attachPointOnGun = gun.Handle:FindFirstChild(attachmentType.."Attachment")
	attachmentBase.CFrame = (attachPointOnGun.WorldCFrame * attachmentBase.AttachmentPosition.CFrame:Inverse())
	local weld = Instance.new("WeldConstraint")
	weld.Parent = attachmentBase
	weld.Part0 = gun.Handle
	weld.Part1 = attachmentBase
--Stat code is skipped
	script.ViewmodelAttachment:FireClient(player,gun,attachment)
end

This works fine, but whenever the viewmodel attachment remote event is fired from any script i’ve tested so far, and the gun is referenced in the local script, this happens:
image
Here’s my localscript handling viewmodel attachments:

script.Parent.ViewmodelAttachment.OnClientEvent:Connect(function(gun,attachment)
	gun.Equipped:Wait()
	
	local viewmodel = game.Workspace.CurrentCamera.ViewmodelStorage[gun.Name]

	local newAttachment = attachment:Clone()
	newAttachment.Parent = viewmodel
	local attachmentBase = newAttachment.AttachmentBase
	local attachmentType = newAttachment.AttachmentInfo.AttachmentType.Value
	newAttachment.Name = attachmentType.."Attachment"

	local attachPointOnGun = viewmodel.Handle:FindFirstChild(attachmentType.."Attachment")
	attachmentBase.CFrame = (attachPointOnGun.WorldCFrame * attachmentBase.AttachmentPosition.CFrame:Inverse())
	local weld = Instance.new("WeldConstraint")
	weld.Parent = attachmentBase
	weld.Part0 = viewmodel.Handle
	weld.Part1 = attachmentBase
end)

Anytime the gun tool is referenced, I get an error. I’m probably doing something horribly wrong, so if you know why this is happening, please tell me, I’m not super good with local scripts, and some information on what I’m doing wrong would be nice. Thanks!

Edit: I think I should add that the attachment is being found, but not the gun, I have no idea why it’s doing this.

Edit 2: Surprisingly wasn’t the fault of the local script, it was the fault of the script creating the gun and calling the module.

i’m going insane trying to debug this, thanks for reading though!

no any reason doing game.Workspace over workspace and more so doing that is bad for perfomance.
You need to do WaitForChild when accesing ReplicatedStorage unless you are using PlayerScript instead of ReplicatedFirst
Also you probably forgot to call require() on modulescript

I didn’t actually forget to require the module, I found the problem, it was with the script creating and calling the module, it only parents the gun to the player AFTER the attachments are applied, after I fixed that it works. I also mainly do game.Workspace out of habit, thanks for helping anyways though!

yeah what i see is is that ur code could be running before ur stuff are added to the game.
try putting a :WaitForChild() on your viewmodel variable.

    local Camera = workspace.CurrentCamera
    local ViewmodelStorage = Camera:WaitForChild("ViewmodelStorage")
	local viewmodel = ViewmodelStorage:WaitForChild(gun.Name)

global workspace is guaranteed to referance workspace even in replicated first.
Index time is far more expensive than having dirrect referance (it being global workspace)
Second luau bytecode interpeter will call GETIMPORT workspace so it will get localized and will get more optimization.

just did before i saw this lol, having another problem but i’ll try and fix it on my own before i post what went wrong

edit: i forgot to use the viewmodel name (“v_”…gun.name and not gun.name), and after that, everything works now

You forgot to remove the square brackets from your example :woozy_face:

sigh… its a tough day at the office :pensive: