Shouldn't this hypothetically work

I am checking if they already have a folder for their effects, yet the script can’t :GetChildren() (later on in the code)

Code:

if not game.Workspace:FindFirstChild(player.Name.." Effects") then
		local Effects = Instance.new("Folder", workspace)
		Effects.Name = player.Name.." Effects"
		Debris:AddItem(Effects,DeathMagicDuration)
		
	else
		local Effects = game.Workspace:FindFirstChild(player.Name.." Effects")
	end
2 Likes

When do you call GetChildren, inside the if check or outside of it?

We need a little bit more context, the code you’re previewing us should be functional, except the else statement where you are setting a local variable and not using it?

1 Like

Outside of it, this check is near the top ish, creates the folder, or finds the other one, and adds the items, and later on gets the children

Are you calling Effects:GetChildren(), it is nil because it is defined in a closure and not the outside scope.

2 things, it is a magic handler, and each magic remote checks for the folder, and yes I am calling that but I didn’t make the folder outside of the remote events for the magic because I don’t have the player so I just thought creating them as they get fired, and keeping the folder for other magic

1 Like

Well does anything happen when you call GetChildren? Is it nil, empty or yields?

attempt to index nil with ‘GetChildren’

1 Like

Can you post a code sample from the start and to the end where you call it?

1 Like

The last line there is the line it errors

Code:

if not game.Workspace:FindFirstChild(player.Name.." Effects") then
		local Effects = Instance.new("Folder", workspace)
		Effects.Name = player.Name.." Effects"
		Debris:AddItem(Effects,DeathMagicDuration)
		
	else
		local Effects = game.Workspace:FindFirstChild(player.Name.." Effects")
	end
	

	local Center = DeathMagicMeshes:WaitForChild("Center"):Clone()
	Center.CFrame = Humrp.CFrame * CFrame.new(0,11.5,-20)
	Center.Orientation = Center.Orientation + Vector3.new(90,0,0)
	Center.Size = Vector3.new(.75,.75,29)
	Center.Parent = Effects

	local Wave1 = DeathMagicMeshes:WaitForChild("Wave1"):Clone()
	Wave1.CFrame = Center.CFrame * CFrame.new(0,0,13.2)
	Wave1.Orientation = Wave1.Orientation - Vector3.new(90,0,0)
	Wave1.Size = Vector3.new(1,.1,1)
	Wave1.Parent = Effects

	local Wave2 = DeathMagicMeshes:WaitForChild("Wave2"):Clone()
	Wave2.CFrame = Center.CFrame * CFrame.new(0,0,11)
	Wave2.Orientation = Wave2.Orientation - Vector3.new(90,0,0)
	Wave2.Size = Vector3.new(1,.1,1)
	Wave2.Parent = Effects

	spawn(function()
		local tempCount = 0

		while wait(1) do
			tempCount = tempCount + 1
			DeathMagicMaxDist = Wave2.Size.X
			DeathMagicDamage = DeathMagicDamage + .5

			for i, plr in pairs(game.Players:GetChildren()) do
				local subChar = plr.Character
				if subChar ~= Character then
					local EHum = subChar:FindFirstChild("Humanoid")
					local Ehumrp subChar:FindFirstChild("HumanoidRootPart")

					if EHum and Humrp then
						local DeathMagicDistance = (Humrp.CFrame.p - Humrp.CFrame.p).Magnitude
						if DeathMagicDistance <= DeathMagicMaxDist then
							EHum:TakeDamage(DeathMagicDamage)
						end
					end
				end
			end

			if tempCount == DeathMagicDuration then
				break
			end
		end
	end)

	local goal = {}
	goal.Size = Center.Size + Vector3.new(1.894,1.894,0)
	local info = TweenInfo.new(.25, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut)
	local tween = TweenService:Create(Center,info,goal)
	tween:Play()

	local goal2 = {}
	goal2.Size = Wave1.Size + Vector3.new(12.826,3.35,12.826)
	local info2 = TweenInfo.new(.20, Enum.EasingStyle.Circular)
	local tween2 = TweenService:Create(Wave1,info2,goal2)
	tween2:Play()

	local goal3 = {}
	goal3.Size = Wave1.Size + Vector3.new(9.906,7.22,10.506)
	local info3 = TweenInfo.new(.20, Enum.EasingStyle.Circular)
	local tween3 = TweenService:Create(Wave2,info3,goal3)
	tween3:Play()
	wait(.50)


	for i, object in pairs(Effects:GetChildren()) do
1 Like

Like I said in the closure, in your code it appears you define Effects as local inside the if statement so it will be not transferred to the outside, you will need do

local Effects = nil
if blah == blah then
   Effects = Statement
end
1 Like

Ah! Thank you! That makes sense, I understood what you said just didn’t know how to work around it!

Now I can keep going! Been scripting all morning

1 Like

Uhh still getting the error actually, yet it is making a folder

1 Like

You should always declare a variable for FindFirstChild then check if it exists:

local Effects = workspace:FindFirstChild("Effects")

if not Effects then
    Effects = Instance.new("Folder")
    Effects.Name = "Effects"
    Effects.Parent = workspace
end

-- code...

^ this format also reduces cognitive complexity

Hi just a quick tip

Setting a parent through Instance.new like this

local foo = Instance.new("Class", bar)

is much slower than setting the object’s parent

local foo = Instance.new("Class")
foo.Parent = bar
1 Like

Interesting, started erroring again, for some reason

What’s the error? Can you show the full code?

1 Like

Error: attempt to index nil with ‘GetChildren’

Here is the code down to the line that it errors
Code:

local Effects = workspace:FindFirstChild(player.Name.." Effects")
	
	if not Effects then
		local Effects = Instance.new("Folder", workspace)
		Effects.Name = player.Name.." Effects"
		--Debris:AddItem(Effects,DeathMagicDuration)
		
	end
	

	local Center = DeathMagicMeshes:WaitForChild("Center"):Clone()
	Center.CFrame = Humrp.CFrame * CFrame.new(0,11.5,-20)
	Center.Orientation = Center.Orientation + Vector3.new(90,0,0)
	Center.Size = Vector3.new(.75,.75,29)
	Center.Parent = Effects

	local Wave1 = DeathMagicMeshes:WaitForChild("Wave1"):Clone()
	Wave1.CFrame = Center.CFrame * CFrame.new(0,0,13.2)
	Wave1.Orientation = Wave1.Orientation - Vector3.new(90,0,0)
	Wave1.Size = Vector3.new(1,.1,1)
	Wave1.Parent = Effects

	local Wave2 = DeathMagicMeshes:WaitForChild("Wave2"):Clone()
	Wave2.CFrame = Center.CFrame * CFrame.new(0,0,11)
	Wave2.Orientation = Wave2.Orientation - Vector3.new(90,0,0)
	Wave2.Size = Vector3.new(1,.1,1)
	Wave2.Parent = Effects

	spawn(function()
		local tempCount = 0

		while wait(1) do
			tempCount = tempCount + 1
			DeathMagicMaxDist = Wave2.Size.X
			DeathMagicDamage = DeathMagicDamage + .5

			for i, plr in pairs(game.Players:GetChildren()) do
				local subChar = plr.Character
				if subChar ~= Character then
					local EHum = subChar:FindFirstChild("Humanoid")
					local Ehumrp subChar:FindFirstChild("HumanoidRootPart")

					if EHum and Humrp then
						local DeathMagicDistance = (Humrp.CFrame.p - Humrp.CFrame.p).Magnitude
						if DeathMagicDistance <= DeathMagicMaxDist then
							EHum:TakeDamage(DeathMagicDamage)
						end
					end
				end
			end

			if tempCount == DeathMagicDuration then
				break
			end
		end
	end)

	local goal = {}
	goal.Size = Center.Size + Vector3.new(1.894,1.894,0)
	local info = TweenInfo.new(.25, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut)
	local tween = TweenService:Create(Center,info,goal)
	tween:Play()

	local goal2 = {}
	goal2.Size = Wave1.Size + Vector3.new(12.826,3.35,12.826)
	local info2 = TweenInfo.new(.20, Enum.EasingStyle.Circular)
	local tween2 = TweenService:Create(Wave1,info2,goal2)
	tween2:Play()

	local goal3 = {}
	goal3.Size = Wave1.Size + Vector3.new(9.906,7.22,10.506)
	local info3 = TweenInfo.new(.20, Enum.EasingStyle.Circular)
	local tween3 = TweenService:Create(Wave2,info3,goal3)
	tween3:Play()
	wait(.50)


	for i, object in pairs(Effects:GetChildren()) do

Is this the line where the error occurs?

2 Likes

Yup, that is the line… Just a second ago it wasn’t but. It doesn’t seem to place the items into the folder

try debugging your code, for example:

if Effects then
   warn("Effects folder found")
   for _, Object in ipairs(Effects:GetChildren()) do
       -- code
   end
else
   warn("Something is wrong with 'Effects' folder")
end