Attempt to index nil with ':GetChildren()'

Hello, my fellow DEVELOPERS!!

So uhh… I’ve been trying to fix the index nil thingy for like 14 minutes, but my Silly Goose of a brain can’t figure out a solution.

I know index nil usually means the variable/object could be nil, but how does that relate to the :GetChildren() function?

Here’s the code!

	local g = false
	local cloud = game:GetService("ServerStorage").Cloud.Cloud:Clone()
	
	for a,z in pairs(cloud.Parent:GetChildren()) do --< the error
		if z:IsA("Part") or z:IsA("UnionOperation") then
			z.Touched:Connect(function(part)
				if part:FindFirstChild("Humanoid") and part.Parent ~= player.Character and g ~= true then
					local victimplayer = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
					victimplayer.Character.Humanoid:TakeDamage(10.9)
				end
			end)
		end
	end

(Also I’m planning to replace the .Touched function since people say its unreliable, so act that does not exist for a moment lol)

1 Like

This happened because the current parent for cloud is nil so you have to set the parent for the cloud first.

local cloud = game:GetService("ServerStorage").Cloud.Cloud:Clone()
cloud.Parent = game.Workspace
1 Like

THANK YOU!
It works now!

Now that I think about it… There was a line of code below the one in post that parents it to the workspace, I Should’ve made the .Touched appear later in the code…

Anyway, hats off to you!

1 Like

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