What do you want to achieve? I am making a egg hatching system
What is the issue?
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if player.Character ~= nil and isHatching == false then
local nearestEgg
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
This is my code the error says: Players.az2166.PlayerGui.EggSystem.ClientScript.HatchClient:134: attempt to index nil with âPositionâ
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if player.Character ~= nil and isHatching == false then
local nearestEgg
local plrPos = player.Character.HumanoidRootPart.Position
for i, v in pairs(Eggs:GetChildren()) do
if nearestEgg == nil then
nearestEgg = v
elseif v.PrimaryPart and nearestEgg.PrimaryPart then
if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
nearestEgg = v
end
end
end
Hello there!
I see that this is an script from egg hatching tutorial, everything in this script that you showed is alright, I think that the problem is not in the script Itâs with something else!
No I sadly canât i need to see the entire scripts models etc. (I have all the scripts done in the tutorial so if you want them contact me in dms) I will still try to find a solution.
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if player.Character ~= nil and isHatching == false then
local nearestEgg
local plrPos = player.Character.HumanoidRootPart
for i, v in pairs(Eggs:GetChildren()) do
if nearestEgg == nil then
nearestEgg = v
else
if (plrPos.Position - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos.Position).Magnitude then
nearestEgg = v
end
end
end
I think it errors because youâre indexing a property.
i fixed it by deleting every other egg and keeping the one im using just for now and now when i go to hatch and press E on the egg it says ServerScriptService.Egg_System.Main_Server:5: attempt to index number with number
and when i press on the error it shows me that it has problem w this part of script
if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
canHatch = true
else
canHatch = false
end
if canHatch == true then
local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("HatchServer"):InvokeServer(nearestEgg)
if result ~= nil then
print("hatch")
hatchOne(result, nearestEgg)
end
else
print("Cannot Hatch")
end
end
end
end)
it has problem on the local result = replicatedStorage:WaitForChild(âEggHatchingRemotesâ):WaitForChild(âHatchServerâ):InvokeServer(nearestEgg)