How to I pass a instance.new var to the else statement?

Hello, I am currently working on a lag test game. I have ran into a problem, for some reason my instance.new var is not defined when using else.
This is my code:

local button = script.Parent
local start = false
local part_amount = 0
button.MouseButton1Click:Connect(function()
	if not start  then
	while true do
		wait(0.1)
		local model = Instance.new('Model', game.Workspace)
		local part = Instance.new('Part', model)
		part.Anchored = false
		part.CanCollide = true
		part.Position = Vector3.new(-303.756, 136.691, 178.5)
		part.Size = Vector3.new(5, 5, 5)
		part.Material = Enum.Material.Glass
		part.Transparency = 0.5
		part_amount += 1
		script.Parent.Parent["Part Amount"].Text = "Parts: "..part_amount
		button.BackgroundColor3 =    Color3.fromHex('#fc0303')
		button.BorderColor3 = Color3.fromHex('#fc0303')
		button.Text = 'Stop'
		end
	else
		model:Destroy() -- The Problem
	end
end)

Here are the workarounds I tried:

local button = script.Parent
local start = false
local part_amount = 0
local model = Instance.new('Model', game.Workspace) --Moved
local part = Instance.new('Part', model) --Moved
button.MouseButton1Click:Connect(function()
	if not start  then
	while true do
		wait(0.1)
		part.Anchored = false
		part.CanCollide = true
		part.Position = Vector3.new(-303.756, 136.691, 178.5)
		part.Size = Vector3.new(5, 5, 5)
		part.Material = Enum.Material.Glass
		part.Transparency = 0.5
		part_amount += 1
		script.Parent.Parent["Part Amount"].Text = "Parts: "..part_amount
		button.BackgroundColor3 =    Color3.fromHex('#fc0303')
		button.BorderColor3 = Color3.fromHex('#fc0303')
		button.Text = 'Stop'
		end
	else
		model:Destroy() -- This is not gonna work
	end
end)
local button = script.Parent
local start = false
local part_amount = 0
button.MouseButton1Click:Connect(function()
	if not start  then
		local model = Instance.new('Model', game.Workspace)
		local part = Instance.new('Part', model)
	while true do
		wait(0.1)
		part.Anchored = false
		part.CanCollide = true
		part.Position = Vector3.new(-303.756, 136.691, 178.5)
		part.Size = Vector3.new(5, 5, 5)
		part.Material = Enum.Material.Glass
		part.Transparency = 0.5
		part_amount += 1
		script.Parent.Parent["Part Amount"].Text = "Parts: "..part_amount
		button.BackgroundColor3 =    Color3.fromHex('#fc0303')
		button.BorderColor3 = Color3.fromHex('#fc0303')
		button.Text = 'Stop'
		end
	elseif start then -- Changed this
		model:Destroy() -- This is not gonna work
	end
end)

I also tried _G but its a local script so It did not work.
Can someone please help?

2 Likes

It should be Model:Destroy() not Model:Destroy

2 Likes

What error are you getting? Method 2 should work. Otherwise, do what @NormalDeveloperX suggested as you’re missing the parenthesis.

2 Likes

The error im getting is that model is not defined.

1 Like

Thanks for helping me catch that, but the error im running into is not defined.

1 Like

Can you show me the error? If that fixed the problem, count it as solution :smiley:

1 Like

Correction: Is it functional? I’m not really understanding the issue currently.

im trying diffrent ways to get it functional, I just need the undefined problem to work where it says this:
Porblem
Its also my 1st post on the devforum so sorry if im not clear.

You haven’t defined model in the correct spot

Define model somewhere else.
I suggest doing it where the function starts:

local button = script.Parent
local start = false
local part_amount = 0
button.MouseButton1Click:Connect(function()
    local model = Instance.new('Model', game.Workspace)
	local part = Instance.new('Part', model)
	if not start  then
	while true do
		wait(0.1)
		part.Anchored = false
		part.CanCollide = true
		part.Position = Vector3.new(-303.756, 136.691, 178.5)
		part.Size = Vector3.new(5, 5, 5)
		part.Material = Enum.Material.Glass
		part.Transparency = 0.5
		part_amount += 1
		script.Parent.Parent["Part Amount"].Text = "Parts: "..part_amount
		button.BackgroundColor3 =    Color3.fromHex('#fc0303')
		button.BorderColor3 = Color3.fromHex('#fc0303')
		button.Text = 'Stop'
		end
	elseif start then 
		model:Destroy() 
	end
end)
2 Likes

Look at @Azul_Litt solution. Pretty sure it should work. :smiley: