Script saying part doesn't have a parent, even though it does

so, for whatever reason this script says this part doesn’t have a parent when it does, I have no idea why this is happening, please help!
script:

for _, Prompt: ProximityPrompt in pairs(ExecutePrompts) do
		print(Prompt:GetFullName())
		if Prompt.Parent then
			print("No parent")
			continue
		end
		if (Prompt.Parent.Position - RootPart.Position).Magnitude > 2 then
			Prompt:Destroy()
		end
	end

output:
image

1 Like

Your logic is wrong.
It should be

if not Prompt.Parent then

(instead of if Prompt.Parent then)

my code it still tweaking, I didn’t notice that, but then this happened:
image
it says no parent

That is correct. The full name is showing that there ExecutePrompt is not parented to anything, which is reflected by the No parent print

I forgot to say I changed it to say what the prompt was, incase it was finding the wrong thing. I tried to see if I could just skip it but:

Did you adjust the code to what @SeargentAUS had recommended? if so then can you show us more of the script, or at least the hierarchy of the prompts so we can have a better idea of the situation

here is more of the script:

for _, Prompt: ProximityPrompt in pairs(ExecutePrompts) do
		print(Prompt.Parent)
		if not Prompt.Parent then
			print("No parent")
			continue
		end
		if (Prompt.Parent.Position - RootPart.Position).Magnitude > 2 then
			Prompt:Destroy()
		end
	end
	local FrontCast = Raycast(RootPart.Position, RootPart.Position + (RootPart.CFrame.LookVector * 2.5),{})
	if FrontCast and FrontCast.Instance.Parent:FindFirstChildWhichIsA("Humanoid") and FrontCast.Instance.Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
		local looking = checkTargetView(FrontCast.Instance.Parent)
		local TheirRootPart = FrontCast.Instance.Parent:FindFirstChildWhichIsA("Humanoid").RootPart
		if looking and TheirRootPart then
			if TheirRootPart:FindFirstChild("ExecutePrompt") == nil then
				local NewExecutePrompt = Instance.new("ProximityPrompt",FrontCast.Instance.Parent:FindFirstChildWhichIsA("Humanoid").RootPart)
				NewExecutePrompt.ActionText = "Execute"
				NewExecutePrompt.RequiresLineOfSight = false 
				NewExecutePrompt.Name = "ExecutePrompt"
				table.insert(ExecutePrompts,NewExecutePrompt)
			end
		end
	end

Are you 100% sure that RootPart exists? Add an if statement to confirm.

yes and uhhh this is wierd:

if #ExecutePrompts > 0 then
		print(ExecutePrompts[1].Parent)
	end

it returns the rootpart

confirmed:
image

Can you add

if not Rootpart then 
    warn("No rootpart")
end

after the no parent check

I figured out a solution. idk why this was happening

for i, V: ProximityPrompt in pairs(ExecutePrompts) do
		local Prompt = ExecutePrompts[i]
		if not Prompt.Parent then
			print("No parent")
			continue
		end
		if (Prompt.Parent.Position - RootPart.Position).Magnitude > 3 then
			table.remove(ExecutePrompts,table.find(ExecutePrompts,Prompt))
			Prompt:Destroy()
			
		end
	end

can you show us this table, or at least the code that makes this table?

This would help us determine whether the script is looping through the right objects or not.

this is the code, I said it above

Ah, glad you figured it out! I believe it was because the variable for the prompt still existed in the table, and so when the script looped through the table it would loop through the empty variable and not find an instance paired to it.
If you already knew this than I apologize, I am just kind of thinking out loud if you know what I mean

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.