Attempt to call a nil value even if the item exists

Hello everyone,

I have a problem with my script, It doesn’t find the child even though it exist.

here is the script:

local ViewFrame = script.Parent
local WeaponFolder = ViewFrame.Weapons:GetChildren()

local event = ViewFrame.Parent.Event

local cam = Instance.new("Camera")
cam.Parent = ViewFrame
ViewFrame.CurrentCamera = cam

	
event.Event:Connect(function(Weapon)
	print(Weapon)
	print(WeaponFolder)
    local Item = WeaponFolder:FindFirstChild(Weapon)
	cam.CFrame =  CFrame.new(Vector3.new(Weapon.Position + Vector3.new(0,2,10)),Weapon.Position)
end)

Here is the error
Players.black1shadow1048.PlayerGui.ScreenGui.CraftingFrame.ViewportFrame.CameraScript:14: attempt to call a nil value

The output from print(Weapon) and print(WeaponFolder):
Spear,
▼ {
[1] = Sword,
[2] = Spear
}

Am I doing something wrong?

Thank you.

local event = ViewFrame.Parent.Event

event.Event:Connect(function(Weapon)
-- whatever code you had
end)

is basically like doing

ViewFrame.Parent.Event.Event:Connect(function(Weapon)
-- whatever code you had
end)

… unless the “Event” in local event = ViewFrame.Parent.Event is meant to be a name.

WeaponFolder is an array because you use the :GetChildren() method

so try to remove :GetChildren() and try again

It didn’t work, I’m really confused

Try

local WeaponFolder = ViewFrame:WaitForChild("Weapons")

oh and also findfirstchild wants a string not an object

local Item = WeaponFolder:FindFirstChild(Weapon.Name)

Which line is line 14 exactly?

this line is the 14th line. I don’t see it’s issue though.

The Weapon value that has been passed to the FindFirstChild Is a string

Okay then just keep it the way that you have it there, but try the other thing i said

So here WeaponFolder is a table of the children contained in your ViewFrame.Weapons instance.
You should remove :GetChildren() to interact with the instance directly or make a new variable that contains the instance and use that instead.

local WeaponFolder = ViewFrame.Weapons
local WeaponFolderChildren = ViewFrame.Weapons:GetChildren()

That doesn’t seem to be the issue. I will try to delete the script and paste it

Edit: Nothing happend

Why do you even have the item variable and If weapon is a string like you say then you can’t do Weapon.Position

from what i read “WeaponFolder” variable is a table and i don’t think you can use FindFirstChild on it maybe try this instead

local Item = WeaponFolder[Weapon]

local Item = Weapons[Weapon] print(Item)
The print(Item) Prints nil

if

local WeaponFolder = ViewFrame.Weapons:GetChildren()

is already folder maybe remove :GetChildren()?
and btw change to your old code too

1 Like

Changed a few things

local ViewFrame = script.Parent
local WeaponFolder = ViewFrame:WaitForChild("Weapons")

local event = ViewFrame.Parent.Event

local cam = Instance.new("Camera")
cam.Parent = ViewFrame
ViewFrame.CurrentCamera = cam

	
event.Event:Connect(function(Weapon : string)
	print(Weapon)
	print(WeaponFolder)
    local Weapon = WeaponFolder:FindFirstChild(Weapon)
    if Weapon then
        cam.CFrame =  CFrame.new(Weapon.Position + Vector3.new(0,2,10),Weapon.Position)
    end
	
end)
1 Like

It seems that I don’t need to use GetChildren() with folders. Thank you so much

1 Like