Collection Service Parent not found

So i keep gettign this error : Players.Berta012oof.PlayerScripts.LocalCannon:72: attempt to index nil with ‘WaitForChild’

I know what it means i debugged it and i find where the problem is i just don’t know how to fix the problem its the parent of the collection i got which is this
image
So the part its the one called cannon inside the other part i think you clerely see i didn’t change parent / called the event at start of the game so i realy don’t know

local CollectService = game:GetService("CollectionService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Store = game:GetService("ReplicatedStorage")

local Remotes = Store:WaitForChild("Remotes")
local CannonEvent = Remotes:WaitForChild("CannonEvent")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humr = Char:WaitForChild("HumanoidRootPart")
local Cannons = CollectService:GetTagged("Cannon")
local Mouse = Player:GetMouse()
local CoolDowns = false

UIS.InputBegan:Connect(function(Input,Obj)
	if Obj == true then return end
	if Input.KeyCode == Enum.KeyCode.E then
		local CannonMag = {}
		local Nearest = 50000000
		local Current
		for i,v in ipairs(Cannons) do
			CannonMag[i] = (Humr.Position - v.Position).Magnitude
		end
		for i,v in pairs(CannonMag) do
			if math.min(v,Nearest) == v then
				Nearest = v
				Current = i
			end 
		end
		if Nearest <= 14 then
			for i,v in ipairs(Cannons) do
				if i == Current then
					if v.Parent.BeingUsed.Value then return end
					if Char:FindFirstChild("Humanoid") then
						Char:FindFirstChild("Humanoid"):UnequipTools()
					end
					CannonEvent:FireServer(v,"Use")
					game.StarterGui:SetCoreGuiEnabled('Backpack',false)
				end
			end
		end
	elseif Input.KeyCode == Enum.KeyCode.Space then
		if Char.Humanoid.Sit == true then
			local CannonMag = {}
			local Nearest = 50000000
			local Current
			for i,v in ipairs(Cannons) do
				CannonMag[i] = (Humr.Position - v.Position).Magnitude
			end
			for i,v in pairs(CannonMag) do
				if math.min(v,Nearest) == v then
					Nearest = v
					Current = i
				end 
			end
			if Nearest <= 14 then
				for i,v in ipairs(Cannons) do
					if i == Current then
						if v.Parent.BeingUsed.Value == false then return end
						CannonEvent:FireServer(v,"Exit")
						game.StarterGui:SetCoreGuiEnabled('Backpack',true)
					end
				end
			end
		end
	end
end)
Mouse.Button1Down:Connect(function()
	if Char.Humanoid.Sit == true then
		for i,v in ipairs(Cannons) do
			print(v.Parent)
			if v.Parent:WaitForChild("Who").Value == Player then
				if CoolDowns == false then
					CoolDowns = true
					CannonEvent:FireServer(v,"Attack")
					wait(3)
					CoolDowns = false
				end
			end
		end
	end
end)

also if you have any way to improve my code i would be realy greatfull since i want everything i do well done and well optimised

1 Like

For any future debuggers his error happens in his button1down event at the very bottom

Oh and btw you cant index through arrays with ipairs iirc, try using just pairs

Screen Shot 2020-12-08 at 3.07.23 PM

I dont really know your setup but assuming you are getting the children only you have no reference to this Parent, v is a collection service it has no parent. It is a data type so this makes no sense

i mean i used it up in the code just fine if you see
image

My bad, (never used collection service assumed it was a array)

Damns but still idk the problem, it should work just fine since its literally the same code just with different child

Perhaps who is replicated on the client?

Why not add an extra if statement checking if the parent exists?

if v.Parent then
    print(v.Parent)
    if v.Parent:WaitForChild(...
1 Like

This is was KINDA the solution in fact it was not the solution alone i also had to move a part of the code the if statement that caused the problem to be moved to server-side thanks tho

1 Like