Why i getting this error?

Hello,

I need to help with this issue.

My local script in StarterPlayerScripts:

--// VARIABLES //--
local Players = game:GetService('Players');
local UserInputService = game:GetService('UserInputService');
local Player = Players.LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:wait();
local PlayerGui = Player:WaitForChild('PlayerGui');
local Game_UI = PlayerGui:WaitForChild('Game_UI');
local Tip_Text = Game_UI.Tip_Text;
local Active = false;
local Object = nil;

--// FUNCTIONS //--
UserInputService.InputBegan:Connect(function(Input)
if (Active and Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Enum.KeyCode.E) then
	Object.Config.Detect_Event:FireServer();
end;
end);

while wait(.05) do
	Object = nil;
	Active = false;
	Tip_Text.Visible = false;
    for _, Window in pairs(game.Workspace:GetChildren()) do
           if Window:FindFirstChild("Center") then
	          local Magnitude = (Character.HumanoidRootPart.Position - Window.PrimaryPart.Position).magnitude;
              if (Magnitude <= Window.Config.Range.Value) then
	              Tip_Text.Text = "Press E to open window";
	              Tip_Text.Visible = true;
	              Active = true;
	              Object = Window;
              end;
           end;
    end;
end;

And here is error from output:

[13:44:21.455 - Players.honza2555.PlayerScripts.Client_Script:25: attempt to index field 'PrimaryPart' (a nil value)](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B8705C2B1%2DBFA4%2D42E1%2D9C90%2DBCCEE249D168%7D&gst=2#25)

13:44:21.456 - Stack Begin

[13:44:21.456 - Script 'Players.honza2555.PlayerScripts.Client_Script', Line 25](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B8705C2B1%2DBFA4%2D42E1%2D9C90%2DBCCEE249D168%7D&gst=2#25)

13:44:21.456 - Stack End

Primary part of model is that part named Center

1 Like

This could be mutiple reasons for this error:

  1. The window isn’t actually a model
  2. The window doesn’t have a primarypart set

Also by the looks of it your trying to verify if it has a primaryparty by a child called center? I would just check if the window is a model and if primarypart is equal to nil
I would just print out which model doesn’t have a primarypart and fix it.

1 Like

This is erroring because the Window, or the object that is currently getting looped doesn’t have a primary part, or maybe isn’t even a model.
You have to add an if statment to check if its a model, and if it has a primary part I assume.

for _, Window in pairs(game.Workspace:GetChildren()) do
           if Window:IsA("Model") then
               if Window.PrimaryPart then
                   if Window:FindFirstChild("Center") then
	                  local Magnitude = (Character.HumanoidRootPart.Position - 
                          Window.PrimaryPart.Position).magnitude;
                      if (Magnitude <= Window.Config.Range.Value) then
	                     Tip_Text.Text = "Press E to open window";
	                     Tip_Text.Visible = true;
	                     Active = true;
	                     Object = Window;
           end
               end
                   end;
                      end;
    end;
end;

Sorry the ends are really wrong devforum should fix indenting :stuck_out_tongue:

1 Like

Yes, from your loop your indexing everything in workspace I would check which part/model it’s erroring on and try to better your checks. for example with checking if it’s a model and had a primarypart

1 Like

print the Window:GetFullName() to see which part is doing this error or as froghopperjacob said just check if the part has a primary part

Its working if i add Window:IsA(“Model”) and Window.PrimaryPart.
Thank you guys :slight_smile:

1 Like

No problem! !!!

1 Like

If the object was not a model, it would give a different error complaining that the object has no such property

Yeah, I assume the problem was that the PrimaryPart didn’t load it yet. But we had a :FindFirstChild() so this is really confusing.

It might still be possible that there is some lonely model that has no PrimaryPart set even though it has a Center child. That would explain why the current fix works and the former did not

1 Like