Part returned as Nil despite loaded already

Hello! I have created a script which detects when a player enters/exits a region, but when I Nil-check, it always returns to Nil despite being loaded already (I have streamingenabled on but the object in question “Hut” is loaded already). The script is as follows:

--------------------
--Define Variables--
--------------------

local Boundries = game.Workspace.Folders.Regions["Area Boundries"]
local Val = 1

local Areas = {
	Hut = Boundries:FindFirstChild("Hut"),
	Home = Boundries:FindFirstChild("Home"),
	Work = Boundries:FindFirstChild("Work")
}

print("Defined Variables!")

-------
--Run--
-------

while true do
	repeat
		if Areas[Val] then
			
			print(Areas[Val].."has loaded locally")
			
			Areas[Val].Touched:Connect(function(hit)
				if hit.Parent == game.Players.LocalPlayer then
					print("Player touched "..Areas[Val].Name.."!")
				
					while true do
						Areas[Val].TouchEnded:Connect(function(hit)
							if hit.Parent == game.Players.LocalPlayer then
								print("Player stoped touching "..Areas[Val].Name.."!")
								return
							end
						end)
						wait(0.1)
					end
					
				end	
			end)
		end
		
		Val = Val + 1
		wait(0.01)
		
	until Val == 4
	
	wait(0.1)
	Val = 1
	
	print("Checked all regions!")
	print(Areas[Val])
end

Note: I may have some other problems with the detection of the player in the script but all that I’m worrying about right now is the Nil-checking.

the print Areas[Val] will print nil because “Val = Val + 1” all the way up to 11, there is only 3 items listed in your dictionary.

1 Like

I didn’t notice that. I’ll check back in once I have it fixed.

I fixed that line, but output is still printing nil. I updated the script in the original post, FYI.

Okay, there is a lot going on, you are running an event in a loop in another loop and then another loop inside the event and another event in that loop. (OOF), is the hut, home and work a part? If so then the line that says

Areas[Val].Touched:Connect(function(hit)

would work, otherwise you would need to loop through its descendants.

Also it prints nil because your val loops until 4 so
“print(Areas[4])” would be nil since you only have Hut, Home and Work

All variables defined from the workspace in this script are parts. Also, I looped until 4 so that Val would have a chance to loop the third time without cutting out the loop early.

Output can’t print nil from Val being 4 because after the repeat loop ends Val would be returned to 1, which would mean that Val would be outputting Areas[1] (hut), effectively confirming that hut is being considered as nil in script, which is why I put the print command there.

Oops, didn’t see that, maybe

So I tried something out, I did this:

local t = {
['a'] = script.Parent
}
print(t['a'])

and it printed the parents name, at the same time I tried this

local t = {

['a'] = script.Parent
}
print(t[1])

and it printed nil, try print(Areas[‘Hut’])

To fix this, don’t index dictionaries with numbers, index them by strings, you can use a table with the names of the elements in the dictionary, example:

local ex = {['string1'] = workspace.Part}
local tab = {ex['string1']}
print(tab[1]) -- workspace.Part
1 Like

Apologies for not replying earlier.

Thanks for helping me solve this! This appears to be working excellently and hopefully I’ll be more closer to completing this than before!