I get the error “attempt to index nil with ‘Position’”
here is the code:
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local runService = game:GetService(“RunService”)
local tweenService = game:GetService(“TweenService”)
local uis = game:GetService(“UserInputService”)
local player = game.Players.LocalPlayer
local Pets = replicatedStorage:WaitForChild(“Pets”)
local Eggs = workspace:WaitForChild(“Eggs”)
local Module3D = require(replicatedStorage:WaitForChild(“Module3D”))
local MaxDisplayDistance = 30
local canHatch = false
task.wait(5)
local function animateBillboard(billboard, openOrClose)
if openOrClose == true then
tweenService:Create(billboard,TweenInfo.new(.2),{Size = UDim2.new(5,0,7,0)}):Play()
else
tweenService:Create(billboard,TweenInfo.new(.1),{Size = UDim2.new(0,0,0,0)}):Play()
wait(.5)
billboard.Enabled = false
end
wait(1)
end
for i, v in pairs(Eggs:GetChildren()) do
local eggPets = Pets:FindFirstChild(v.Name)
print("EggPets: ",eggPets)
print("Eggs: ",Eggs)
print("Pets: ",Pets)
if eggPets ~= nil then
local billboardTemp = script.Template:Clone()
local container = billboardTemp:WaitForChild("Container")
local MainFrame = container:WaitForChild("MainFrame")
local template = MainFrame:WaitForChild("Template")
local display = template:WaitForChild("Display")
billboardTemp.Parent = script.Parent.Parent.EggBillboards
billboardTemp.Name = v.Name
billboardTemp.Adornee = v.EggMesh
billboardTemp.Enabled = true
local pets = {}
for x, pet in pairs(eggPets:GetChildren()) do
table.insert(pets,pet.Rarity.Value)
end
table.sort(pets)
for i = 1, math.floor(#pets/2) do
local j = #pets - i + 1
pets[i], pets[j] = pets[j], pets[i]
end
for _, rarity in pairs(pets) do
for _, pet in pairs(eggPets:GetChildren()) do
if pet.Rarity.Value == rarity then
local rarity = pet.Rarity.Value
local clonedTemp = template:Clone()
clonedTemp.Name = pet.Name
clonedTemp.Rarity.Text = tostring(pet.Rarity.Value).."%"
clonedTemp.Visible = true
clonedTemp.Parent = MainFrame
local petModel = Module3D:Attach3D(clonedTemp.Display,pet:Clone())
petModel:SetDepthMultiplier(1.2)
petModel.Camera.FieldOfView = 5
petModel.Visible = true
runService.RenderStepped:Connect(function()
petModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
end)
break
else
continue
end
end
end
runService.RenderStepped:Connect(function()
if player:DistanceFromCharacter(v.EggMesh.Position) < MaxDisplayDistance then
print("egg near")
canHatch = true
billboardTemp.Enabled = true
animateBillboard(billboardTemp, true)
else
animateBillboard(billboardTemp, false)
canHatch = false
end
print(canHatch)
end)
end
end
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if canHatch == true and player.Character ~= nil then
local nearestEgg = nil
local plrPos = player.Character.HumanoidRootPart.Position
for i, v in pairs(Eggs:GetChildren()) do
if nearestEgg == nil then
nearestEgg = v
else
if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
nearestEgg = v
end
end
end
print("can hatch")
print(nearestEgg)
else
print("cant hatch")
end
end
end)
I don’t know why this is happening, there are EggMeshes in both Eggs (which is how the guis show up), I repeated this tutorial (https://www.youtube.com/watch?v=f0D10_khKzg&list=PLH1di03gos6YWEp6-hcq3b7oQfqilRaoW&index=4) so many times and still can’t find out why my code has an error while the one on the tutorial doesn’t. Anyone?