Objects won't follow mouse in a placement system

My objects won’t follow the mouse when I make a placement system. Before, when experimenting with one object, it worked fine, now it seems that the objects just get cloned and stay in the same position. I used a text box, so the user can type in the object’s name. I have already checked the names, as well as printed out statements, and everything is correct.

There is an error:
Players.Awesomemode14.PlayerGui.PlacementScript:34: attempt to call a nil value

Here is part of of my code:

textbox.FocusLost:Connect(function(enterPressed)
	if (enterPressed) then
		if (objectFolder:FindFirstChild(textbox.Text)) then
			local obj = objectFolder:FindFirstChild(textbox.Text)
			local newObj = obj:Clone()
			newObj.Parent = game.Workspace
			part = newObj
			startPlace = true
		end
	end
end)

while true do
	wait()
	if startPlace == true then
		CalculatePosition(part) --here is the error (line 34)
	end
end

function CalculatePosition(model)
	CheckCollisions(model)
	local x = math.floor(mouse.Hit.X / grid + 0.5) * grid
	--local y = math.floor(mouse.Hit.Y / grid + 0.5) * grid
	local y = 4
	if y <= 4 then
		y = 4
	end
	local z = math.floor(mouse.Hit.Z / grid + 0.5) * grid
	model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:Lerp(CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation * rotationDegree), 
		0), speed))
end

Ask if you need more info.

Thanks for the help!

This is because you have the function after the while wait() do. Move the function to before it.

Thank you, is there an explanation; from what I know is it because the code is running order and while true do just blocks rest of the code? Also why do I get an error when I try to cancel, is there a way to check and block the error:

The error: attempt to index nil with ‘Touched’

function CheckCollisions(model)
  if model ~= nil then
  	local collisionPoint = model.PrimaryPart.Touched:Connect(function() end) --this is the error
  	local touchingParts = model.PrimaryPart:GetTouchingParts()
  	canPlace = true
  	for i = 0, #touchingParts do
  		if touchingParts[i] ~= nil then
  			if not touchingParts[i]:IsDescendantOf(model) and not touchingParts[i]:IsDescendantOf(character) then
  				canPlace = false	
  			end			
  		end
  	end
  	collisionPoint:Disconnect()		
  end
end

function CancelPlacement()
   part:Destroy()
end