Help with variable not behaving properly?

For some reason, this local variable that I made doesn’t behave properly? I don’t know how to explain it in a short way. I am trying to create a command that creates a red circle around the player.

Code:

		local RedZone 
		local Following 
	
		if string.lower(Radius[2]) == "destroy" and RedZone then
			print("ok")
			RedZone:Destroy()
			Following = false
		elseif string.lower(Radius[2]) ~= "destroy" then
			RedZone =  Instance.new("Part")
			Following = true
			
			print(Radius[2])
			
			RedZone.Anchored = true
			RedZone.BrickColor = BrickColor.new("Really red")
			RedZone.CanCollide = false
			RedZone.Shape = Enum.PartType.Cylinder
			RedZone.Material = Enum.Material.Neon
			RedZone.Transparency = 0.75
			RedZone.Size = Vector3.new(0.1,Radius[2],Radius[2])
			RedZone.CFrame = (Character.HumanoidRootPart.CFrame * CFrame.new(0,-Character.Humanoid.HipHeight - 1,0)) * CFrame.Angles(0,0,math.rad(90))
			RedZone.Parent = Character.HumanoidRootPart
			
			while Following do
				RedZone.CFrame = (Character.HumanoidRootPart.CFrame * CFrame.new(0,-Character.Humanoid.HipHeight - 1,0)) * CFrame.Angles(0,0,math.rad(90))
				RunService.Heartbeat:Wait()
			end
		end

When I delete the RedZone variable (the local variable that is nil originally) from the script, this happens:


Aren’t all the code under the elseif supposed to be highlighted in red too?
Same thing with the Following variable.

Plus, when I actually test out the command, even with the variables deleted, the script still works. The red circle is created and follows me. However, when I try to use the “destroy” command, it does not work unless RedZone is defined. Even if it is, it prints “ok” but does not delete the part or set Following to false. Really confused here. Sorry if it’s some sort of obvious mistake but please do point it out if it is!

I don’t think the ROBLOX engine automatically declares a variable with no value as nil.

Just like in the script, you declared local RedZone, but the engine doesn’t accept that.

The workaround is to declare the variable with nil. That is, local RedZone = nil.

There is also an error when this happens:

Thanks for trying to help, but I already tried that :frowning:

	local RedZone = nil
	local Following = false

	if Radius[2] == "destroy" then
		print("ok")
		RedZone:Destroy()
		Following = false
	elseif Radius[2] == "add" then
		Following = true
		
		RedZone = Instance.new("Part")

I get an error at RedZone:Destroy() because RedZone is nil.

You could check if RedZone is not nil. If it is not nil, destroy it; otherwise, don’t do anything.

I am not really sure on how you will declare RedZone with a new value in the script.

Just tried it, “ok” does not print anymore because I would assume that RedZone is still nil, and nothing happens.