Attempt to index nil with position

I got a problemwith determinating an existing part′s Position with a LocalScript.

local miesto = math.random(1, 1)
if miesto == 1 then
	script.Parent.Value = "Talin-KCTWaste"
	wait(1)
	script.km.Value = (game.Workspace.KTCTalin:FindFirstChild("Road").Position.X-game.Workspace.KTCTalinWaste:FindFirstChild("Road").Position.X)-(game.Workspace.KTCTalin:FindFirstChild("Road").Position.Z-game.Workspace.KTCTalinWaste:FindFirstChild("Road").Position.Z)
end

I tried putting wait(1), :FindFirstChild("Road"). Doing it in command bar seems fine.

I would recommend adding a FindFirstChild in an if statement to check that the part exists and try using WaitForChild instead of FindFirstChild to make sure the part has fully loaded before it checks for the position of the part.

1 Like

Try using FindFirstChild() or ‘WaitForChild()’

FindFirstChild(): Returns the first child of the Instance found with the given name.

WaitForChild(): Returns the child of the Instance with the given name. If the child doesn’t exist, it will yield the current thread until it does.

If your going to use FindFirstChild() the code would be something like this:

local miesto = math.random(1, 1)
local _workspace = game:GetService("Workspace")

if miesto == 1 then
	local Road = _workspace.KTCTalin:FindFirstChild("Road")
	script.Parent.Value = "Talin-KCTWaste"
	wait(1)
	if Road then -- Check if 'road' exists
		script.km.Value = (Road.Position.X-Road.Position.X)-(Road.Position.Z-Road.Position.Z)
	end
end

Or if your going to use WaitForChild()

local miesto = math.random(1, 1)
local _workspace = game:GetService("Workspace")
if miesto == 1 then
	local KTCTalin = _workspace:WaitForChild("KTCTalin")
	local Road = KTCTalin:WaitForChild("Road")
	script.Parent.Value = "Talin-KCTWaste"
	wait(1)
	script.km.Value = (Road.Position.X-Road.Position.X)-(Road.Position.Z-Road.Position.Z) -- Use the 'Road' variable instead.
end

Leave feedback if there are any errors, and i’ll try to fix it.

1 Like

yeah… FindFirstChild() works better for this one

local miesto = math.random(1, 1)
local workspace = game:GetService("Workspace")

if miesto == 1 then
	local KTCTalin = workspace:FindFirstChild("KTCTalin")

	if KTCTalin then
		local Road = KTCTalin:FindFirstChild("Road")

		if Road then
			script.Parent.Value = "Talin-KCTWaste"
			wait(1)
			local xDifference = Road.Position.X - Road.Position.X
			local zDifference = Road.Position.Z - Road.Position.Z
			script.km.Value = xDifference - zDifference
		else
			warn("road not found")
		end
	else
		warn("ktctalin not found")
	end
end

Ask if there are still errors

1 Like

image

Also sorry that it took me a while to reply.

The error is quite apparent. KTCTalin.Road doesn’t exist when you try to call it, hence the code doesn’t proceed (or errors). If it is supposed to exist or is created by another script, it may help to check what’s wrong with the object’s creation or existence first.

When you’re playing, check the workspace to see if the road is there. If not, it might be getting destroyed somehow

I tried making this script:

local miesto = math.random(1, 1)
if miesto == 1 then
	for i = 1, 1000 do
		print("Attempt number "..i)
		local Indicator = game.Workspace.KTCTalin:FindFirstChild("Road")
		local IndicatorB = game.Workspace.KTCTalinWaste:FindFirstChild("Road")
		if Indicator and IndicatorB then
			script.Parent.Value = "Talin-KCTWaste"
			script.km.Value = (game.Workspace.KTCTalin:FindFirstChild("Road").Position.X-game.Workspace.KTCTalinWaste:FindFirstChild("Road").Position.X)-(game.Workspace.KTCTalin:FindFirstChild("Road").Position.Z-game.Workspace.KTCTalinWaste:FindFirstChild("Road").Position.Z)
			print(i.." total attempts.")
		else
			wait(1)
		end
	end
end

And it looks like it triggers after ~60 seconds.

The road exists, it is not being destroyed (atleast by my scripts)…

Looks like the problem was with Streaming in Workspace, because it didnt load in the roads.

1 Like

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