Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index nil with 'GetChildren' ?

commands.endround = function(sender) -- End round command
	
	print("End round command fired by "..sender.Name)
	
	local map = game.Workspace.ChosenMap:GetChildren()
	
	map.Gems:GetChildren():Destroy()
	
	print("Round ended")
end

This is a command I made to end the current round. The command its self works except for one part-line 7-"Attempt to index nil with 'GetChildren'"

When the main script spawns in the map, it gets put into the chosen map folder. Every map has a gems folder with all the gems that need to be found, they get destroyed once found. When all the gems in the gems folder are found, the round automatically ends. So this script is supposed to destroy the gems that way the other script (the main script) can end the round. But I get an error, why is this?

Also can someone please tell me what attempt to index nil means?

Thanks!

4 comments

0

I've had this problem like 20 times and I still don't remember how I got around it...

1

Can I see where you're running the function?

0

The function runs when you say "/endround" which works because it prints out "End round command fired by [name]"

0

Also I was following a tutorial, Alvin_Blox admin commands tutorial.

1 answer

I fail to see why you would need to use GetChildren at line 5, considering that you are only looking for the gems folder. Instead, try referring to the folder directly: local map = game.Workspace.ChosenMap In short, remove GetChildren at the end of line 5

It is worth noting that even though the map variable in your original script contains the gems folder as part of a list, referring to it the way you did does not work and will always return nil. I.e, map.Gems returns nil, which is the cause of the error message.

Even with this change, however, there is still a problem with your script that, if run, would return an error. Remember that GetChlidren returns a list with all the children of an object. This means that you would not be able to do map.Gems:GetChildren():Destroy() as the Destroy function can only be performed to objects, not datatypes. Instead, you would need to iterate through every value contained in the list returned by GetChildren. This can be done with the following script:

local gems = map.Gems:GetChildren()
for _, gem in pairs(gems) do
	gem:Destroy()
end

With both of these changes, the full script would look like following:

commands.endround = function(sender) -- End round command

	print("End round command fired by "..sender.Name)

	local map = game.Workspace.ChosenMap
	 
	local gems = map.Gems:GetChildren()
	for _, gem in pairs(gems) do
		gem:Destroy()
	end

	print("Round ended")
end

1 comment

0

The chosen map folder is the folder where the chosen map goes. If I use map:FindFirstCihld() to find the map, I will get an error. If I use map:GetChildren() to find the map, I will also get an error. And inside the map is a folder called gems where all of them are stored.

This discussion is preserved as it was. New answers, comments, and votes are closed.