Attempt to index nil with 'Remove'

Hey, I have a system that in certain time in the game a HighlightTemplate gets cloned onto a player so they glow and after 5 seconds it should get destroyed… that works! But for some reason it always prints error in the console after the deletion of the highlight.

My script (which is a child of the HighlightTemplate):

local checkInterval = 1

local function checkLocation()
	local parent = script.Parent and script.Parent.Parent
	if parent and parent:IsA("ReplicatedStorage") then
	else
		wait(5)
		script.Parent:Remove()
	end
end

while true do
	checkLocation()
	wait(checkInterval)
end

The error: attempt to index nil with ‘Remove’

I already tried to do everything I could to fix it, but still for some reason the script.Parent is nil and it cannot be removed with Remove() or even with Destroy()

Well i think you need to use :Destroy() or add a check before like:

if not script.Parent then return end
2 Likes

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