Issue with nil?

		if data[1] == nil then return end
		if data[1].Parent == nil then return end
			if data[1].Parent.Name == "Urgent_1" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			end
-- line 128 error here
			if data[1].Parent.Name == "Urgent_2" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			end
			if data[1].Parent.Name == "Urgent_3" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			end
			if data[1].Parent.Name == "Urgent_4" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			end

I’ve got this code, at the top it checks if data[1] or it’s parent are nil and if so return out of the function. But somehow I still get this error:

Players.matt1020304050.PlayerScripts.IndicatorsClient:128: attempt to index nil with ‘Name’

This code is inside a function running in renderstepped

Any ideas how to fix? I can show whole code if needed.

You are destroying the data[1] then checking it’s parent’s name. Destroy() will set the parent to nil.

By your code sctructure I assume you mean to be using elseif to chain multiple conditions and only have one process.

if data[1] == nil then return end
		if data[1].Parent == nil then return end
			if data[1].Parent.Name == "Urgent_1" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			elseif data[1].Parent.Name == "Urgent_2" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			elseif data[1].Parent.Name == "Urgent_3" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			elseif data[1].Parent.Name == "Urgent_4" and (position - player.Character.Head.Position).magnitude <= 20 then
				data[1]:Destroy()
			end

Ohhh, I see. Thanks! ////////,…//////

if data[1].Parent.Name:find("Urgent")  and (position - player.Character.Head.Position).magnitude <= 20 then
local ValidNames = {"Urgent_1", "Urgent_2" "Urgent_3", "Urgent_4"}
if table.find(ValidNames, data[1].Parent.Name) and (position - player.Character.Head.Position).magnitude <= 20 then
-- If you know what data[1] & data[1].Parent could be then make variables for them
local Urgent = data[1].Parent
if Urgent.Name:find("Urgent") and (position - player.Character.Head.Position).magnitude <= 20 then

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