FindFirstChild() returning nil when the child exists

So basically, i have a system that picks a weapon from a list and give the player it but for some reason when i try find it with FindFirstChild() it returns nil when the child exists

not the full script but its what is giving me the issue, dont be scared to ask for the full thing!
added some comments to help understanding it better ig

		local MTMax = table.maxn(MTable) --finds the total amount of weapons there is
		
		local Backpack = Player.Backpack
		local meleeRNG = math.random(1,MTMax) --gets a random number based on the total of weapons there are
		
		for i,v in pairs(MTable) do  --goes through the table which has all the weapons
			local choosenMelee = MTable[meleeRNG] --picks a weapons index with the RNG
			print(choosenMelee) --prints the weapon with that index
			local newMelee = Melee:FindFirstChild(choosenMelee) --finds the weapon in the weapons folder in ServerStorage
			print(newMelee) --should be printing the weapons name like the other print but instead prints nil
		end
	end
end)

Can send full code?

Things I would check is if the child exists in test mode at the time the function is called.
Secondly what object is “Melee” linked to? Could be wrong path.
Also chosenMelee doesn’t print nil, yes?

1 Like

newMelee is the one that prints nil
choosenMelee just prints the weapons name as normal, in this case “Knife”
Melee a folder in ServerStorage with all the melee weapons

--// Services
local SS = game:GetService("ServerStorage")
local Players = game:GetService("Players")

--// Variables
local Primary = SS:WaitForChild("Primary")
local Secondary = SS:WaitForChild("Secondary")
local Melee = SS:WaitForChild("Melee")
local Throwable = SS:WaitForChild("Throwable")

local PTable = {}
local STable = {}
local MTable = {}
local TTable = {}

--// Script
Players.PlayerAdded:Connect(function(Player)
	for i,v in pairs(Primary:GetChildren()) do
		table.insert(PTable, v)
	end
	
	for i,v in pairs(Secondary:GetChildren()) do
		table.insert(STable, v)
	end
	
	for i,v in pairs(Melee:GetChildren()) do
		table.insert(MTable, v)
	end
	
	for i,v in pairs(Throwable:GetChildren()) do
		table.insert(TTable, v)
	end
	
	if Player then
		local PTMax = table.maxn(PTable)
		local STMax = table.maxn(STable)
		local MTMax = table.maxn(MTable)
		local TTMax = table.maxn(TTable)
		
		local Backpack = Player.Backpack
		local meleeRNG = math.random(1,MTMax)
		
		for i,v in pairs(MTable) do 
			local choosenMelee = MTable[meleeRNG]
			print(choosenMelee) 
			local newMelee = Melee:FindFirstChild(choosenMelee)
			print(newMelee)
		end
	end
end)

This is because the table is containing the objects themselves instead of their names.
So whenever you index choosenMelee, that is the object. You can’t do Melee:FindFirstChild(choosenMelee) because choosenMelee isn’t a string.

1 Like

Didnt realise that lol
i replaced the line with local newMelee = Melee:FindFirstChild(tostring(choosenMelee)) and it works fine now, thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.