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?