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
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