Clone not working if set outside of Mouse.Changed

I’m making a preview so you can see where your emplacement is being placed.
This works, but clones something everytime the mouse is moved, but when I move the clone = val:Clone()
line outside of mouse.Move, it doesn’t show up at all.

function PreviewF(val)
	local clone
	if script.Parent.DigMode.Value ~= true and val ~= nil then
		spawn(function()
			
			mouse.Move:Connect(function()
				print("this happens")
				preview = true
				clone = val:Clone()
				clone.Parent = game.Workspace
				clone:TranslateBy(mouse.Hit.p)
				local rotation = CFrame.Angles(0, math.rad(Player.Character.HumanoidRootPart.Orientation.Y), 0)
				local modelCFrame = clone:GetPrimaryPartCFrame()
				clone:SetPrimaryPartCFrame( modelCFrame * rotation )
				for i,v in pairs(clone:GetDescendants()) do
					pcall(function()
						v.Transparency = 0.5
						v.CanColide = false
						v.Anchored = true
					end)
				end
				
			end)
		end)
	elseif preview == true then
		pcall(function()
			clone:Destroy()
			preview = false
		end)
	end
end

Try making that:

if clone == nil then
    clone = val:Clone()
    clone.Parent = game.Workspace
    clone:TranslateBy(mouse.Hit.p)
end
1 Like

Hey that works, doesn’t move and spins really fast in a circle but it works.
Full code:

function PreviewF(val)
	local clone
	if script.Parent.DigMode.Value ~= true and val ~= nil then
		spawn(function()

			mouse.Move:Connect(function()
				print("this happens")
				preview = true
				if clone == nil then
					clone = val:Clone()
					clone.Parent = game.Workspace
					clone:TranslateBy(mouse.Hit.p)
				end
				local rotation = CFrame.Angles(0, math.rad(Player.Character.HumanoidRootPart.Orientation.Y), 0)
				local modelCFrame = clone:GetPrimaryPartCFrame()
				clone:SetPrimaryPartCFrame( modelCFrame * rotation )
				for i,v in pairs(clone:GetDescendants()) do
					pcall(function()
						v.Transparency = 0.5
						v.CanColide = false
						v.Anchored = true
					end)
				end

			end)
		end)
	elseif preview == true then
		pcall(function()
			clone:Destroy()
			preview = false
		end)
	end
end

intended goal is for it to be at the position of the mouse but the y axis of the humanoid